-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainMenu.py
More file actions
131 lines (109 loc) · 4.85 KB
/
Copy pathMainMenu.py
File metadata and controls
131 lines (109 loc) · 4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
from PyQt5 import QtWidgets, uic, QtCore
from PyQt5.QtCore import QDir
from PyQt5.QtWidgets import QVBoxLayout, QFileDialog
from Functions.imageConvertionFuncs import *
from Functions.errorMessages import *
from Functions.algorithms import *
from Functions.resizeImage import *
from Functions.checksum import *
from os import path
from PIL import Image
class MainMenu(QtWidgets.QMainWindow):
def __init__(self):
super(MainMenu, self).__init__() # Call the inherited classes __init__ method
uic.loadUi('dataSecurity.ui', self) # Load the .ui file
# Class variables definition (Optional but the vars definitions below necessary)
self.encryptedImg = self.binaryImg = self.decipherImg = None
self.checksumArr = []
self.submitted = False
improveRuntimeReply = QMessageBox.question(self, "Before we get started!",
"Do you want to improve runtime? Note that data can be lost.",
QMessageBox.Yes, QMessageBox.No, )
if improveRuntimeReply == QMessageBox.Yes:
self.shrinkImages = True
else:
self.shrinkImages = False
# Widgets definitions
vbox = QVBoxLayout(self)
self.errDialog = QtWidgets.QErrorMessage() # self because it called in another function
# Organize the title alignment
self.Title.setAlignment(QtCore.Qt.AlignCenter)
# upload color image
vbox.addWidget(self.ColorImageBtn)
self.ColorImageBtn.clicked.connect(lambda: self.loadImage('c'))
# upload back & white image
vbox.addWidget(self.BinaryImageBtn)
self.BinaryImageBtn.clicked.connect(lambda: self.loadImage('b'))
# Submit button
vbox.addWidget(self.SubmitBtn)
self.SubmitBtn.clicked.connect(lambda: self.handleSubmit())
# decipher button
vbox.addWidget(self.DecipherBtn)
self.DecipherBtn.clicked.connect(lambda: self.handleDecipher())
# Show the GUI
self.show()
def loadImage(self, t):
name = QFileDialog.getOpenFileName(self, 'Select Color Image', QDir.currentPath(),
"Image files (*.jpg, *.gif, *.png)")
path = name[0]
print(name[0])
if name:
if self.shrinkImages:
path = reduceImage(path)
if t == 'b':
self.binImgPath = path
self.binaryImg, self.binImgObj = binaryConvert(self.binImgPath)
# print(self.binaryImg)
self.checksumArr = createChecksum(self.binaryImg)
# print(self.checksumArr)
else:
self.colorImgPath = path
self.encryptedImg, self.colorImgObj = RGBConvert(self.colorImgPath)
# print(self.colorImg)
return
def handleSubmit(self):
try:
if self.encryptedImg is None or self.binaryImg is None:
raise NotImplementedError
elif not checkImagesSize(self.binImgObj, self.colorImgObj):
raise ValueError
else:
print('Successfully received input, implementing algorithm...')
# If everything is ok, embed the binary image into the color image
self.encryptedImg = embeddingAlgorithm(self.encryptedImg, self.binaryImg)
# show results
img = arrToImage(self.encryptedImg, 'RGB')
img.show()
self.submitted = True
except NotImplementedError:
errorMessage('Binary & Color images must be uploaded!')
except ValueError:
errorMessage('Binary image must be smaller than color image!')
finally:
# Break function in any case
return
def handleDecipher(self):
try:
if not self.submitted and path.exists("../encryptedImage.png"):
raise NotImplementedError
except NotImplementedError:
errorMessage('Encryption must be done before deciphering!')
# If submitted
else:
if path.exists("../encryptedImage.png"):
self.decipherImg = Image.open("../encryptedImage.png")
img = arrToImage(self.decipherImg, 'RGB')
img.show()
else:
self.decipherImg = HVFlip(reconstructedAlgorithm(self.encryptedImg, self.binaryImg))
testResult = testChecksum(self.decipherImg, self.checksumArr)
if testResult==False:
print(testResult)
errorMessage("Checksum is not valid!")
else:
print(testResult)
img = arrToImage(self.decipherImg, 'L')
img.show()
finally:
# Break function in any case
return