-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
289 lines (251 loc) · 11.1 KB
/
main.py
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
'''
LSB Steganography program in Python 3
Requirements: OpenCV, NumPy, tkinter , Pillow
Author:Tushar & Sourav
'''
from tkinter import *
from tkinter import ttk
import tkinter.filedialog
from PIL import ImageTk
from PIL import Image
from tkinter import messagebox
from io import BytesIO
import os
class Stegno:
art ='''
░██████╗░░█████╗░ ░█████╗░███╗░░██╗
██╔════╝░██╔══██╗ ██╔══██╗████╗░██║
██║░░██╗░██║░░██║ ██║░░██║██╔██╗██║
██║░░╚██╗██║░░██║ ██║░░██║██║╚████║
╚██████╔╝╚█████╔╝ ╚█████╔╝██║░╚███║
░╚═════╝░░╚════╝░ ░╚════╝░╚═╝░░╚══╝ '''
art2 = '''⠀⠀⠀⠀⠀⠀⠀⢀⣀⣤⣤⣤⣄⣀⠀⠀⠀⠀⢀⣠⣤⡤⠄⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⢀⣴⣿⣿⣿⣿⣿⣿⣿⣿⣶⣤⠖⠉⠀⢀⣀⣀⣀⣀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⣰⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠋⠀⣠⣾⣿⣿⣿⡿⠿⢿⣶⣤⡀⠀⠀⠀
⠀⣰⣿⣿⣿⠟⠛⠛⠛⠻⠿⠿⢿⠇⠀⢰⣿⣿⣍⣀⣤⣤⣴⣾⣿⣿⣿⡄⠀⠀
⣾⣿⣿⣿⣄⣠⣤⣤⣤⣤⣤⣀⡄⠀⠀⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠀⠀
⣿⣿⣿⡿⠋⠁⠀⠈⠉⠉⢁⣄⠀⠀⠀⢿⣿⣿⣿⣿⡟⠀⠀⢸⣿⣿⣿⣿⠀⠀
⣿⣿⣿⣦⣤⣤⣄⣀⣀⣠⣼⠁⠀⠀⠀⠘⣿⣿⣿⣿⣷⣤⣤⣾⣿⣿⣿⡿⠀⠀
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣷⣾⣿⣿⡄⠀⠘⠿⣿⣿⣿⣿⣿⣿⣿⣿⠟⠁⠀⠀
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡏⣡⣦⣶⣄⠀⠈⠉⠛⠛⠻⠛⠋⠁⠀⠀⠀⠀
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡻⣻⣿⣿⣿⣶⣤⣄⣀⣤⠀⠀⠀⠀⠀⠀⠀
⣿⣿⣿⣿⣦⣀⠀⠉⠉⠉⠉⠉⠉⠙⠛⠛⠛⠛⠛⠛⠛⠛⠛⠀⠀⠀⠀⠀⣀⡀
⣿⣿⣿⣿⣿⣿⣿⣿⣶⣶⣶⣶⣶⣶⣶⣶⣶⣶⣶⣶⣶⣶⣶⠄⢀⣴⣾⣿⣿⣷
⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠏⠀⣾⣿⣿⣿⣿⣿
⠈⠉⠉⠛⢻⣿⡉⠙⠿⣿⣿⣿⣿⣿⣿⠿⠿⠿⠟⠛⠉⠀⠀⠀⠙⠿⣿⣿⠿⠋
⠀⠀⠀⠀⢸⣿⣿⣷⣶⣾⣿⣿⣿⣿⡏⠀⠀⠀⠀⠀⠀⠀⣰⣷⣦⠀⠀⠀⠀
'''
output_image_size = 0
def main(self,root):
root.title('ImageSteganography')
root.geometry('1920x1080')
root.resizable(width =False, height=False)
f = Frame(root)
title = Label(f,text='Image Steganography')
title.config(font=('courier',33))
title.grid(pady=10)
b_encode = Button(f,text="Encode",command= lambda :self.frame1_encode(f), padx=14)
b_encode.config(font=('courier',14))
b_decode = Button(f, text="Decode",padx=14,command=lambda :self.frame1_decode(f))
b_decode.config(font=('courier',14))
b_decode.grid(pady = 12)
ascii_art = Label(f,text=self.art)
ascii_art.config(font=('courier',60))
ascii_art2 = Label(f,text=self.art2)
ascii_art2.config(font=('courier',12,'bold'))
root.grid_rowconfigure(1, weight=1)
root.grid_columnconfigure(0, weight=1)
f.grid()
title.grid(row=1)
b_encode.grid(row=2)
b_decode.grid(row=3)
ascii_art.grid(row=4,pady=10)
ascii_art2.grid(row=5,pady=5)
def home(self,frame):
frame.destroy()
self.main(root)
def frame1_decode(self,f):
f.destroy()
d_f2 = Frame(root)
label_art = Label(d_f2, text='٩(^‿^)۶')
label_art.config(font=('courier',90))
label_art.grid(row =1,pady=50)
l1 = Label(d_f2, text='Select Image with Hidden text:')
l1.config(font=('courier',18))
l1.grid()
bws_button = Button(d_f2, text='Select', command=lambda :self.frame2_decode(d_f2))
bws_button.config(font=('courier',18))
bws_button.grid()
back_button = Button(d_f2, text='Cancel', command=lambda : Stegno.home(self,d_f2))
back_button.config(font=('courier',18))
back_button.grid(pady=15)
back_button.grid()
d_f2.grid()
def frame2_decode(self,d_f2):
d_f3 = Frame(root)
myfile = tkinter.filedialog.askopenfilename(filetypes = ([('png', '*.png'),('jpeg', '*.jpeg'),('jpg', '*.jpg'),('All Files', '*.*')]))
if not myfile:
messagebox.showerror("Error","You have selected nothing !")
else:
myimg = Image.open(myfile, 'r')
myimage = myimg.resize((300, 200))
img = ImageTk.PhotoImage(myimage)
l4= Label(d_f3,text='Selected Image :')
l4.config(font=('courier',18))
l4.grid()
panel = Label(d_f3, image=img)
panel.image = img
panel.grid()
hidden_data = self.decode(myimg)
l2 = Label(d_f3, text='Hidden data is :')
l2.config(font=('courier',18))
l2.grid(pady=10)
text_area = Text(d_f3, width=50, height=10)
text_area.insert(INSERT, hidden_data)
text_area.configure(state='disabled')
text_area.grid()
back_button = Button(d_f3, text='Cancel', command= lambda :self.page3(d_f3))
back_button.config(font=('courier',11))
back_button.grid(pady=15)
back_button.grid()
show_info = Button(d_f3,text='More Info',command=self.info)
show_info.config(font=('courier',11))
show_info.grid()
d_f3.grid(row=1)
d_f2.destroy()
def decode(self, image):
data = ''
imgdata = iter(image.getdata())
while (True):
pixels = [value for value in imgdata.__next__()[:3] +
imgdata.__next__()[:3] +
imgdata.__next__()[:3]]
binstr = ''
for i in pixels[:8]:
if i % 2 == 0:
binstr += '0'
else:
binstr += '1'
data += chr(int(binstr, 2))
if pixels[-1] % 2 != 0:
return data
def frame1_encode(self,f):
f.destroy()
f2 = Frame(root)
label_art = Label(f2, text='''
+--+--+--+--+--+--+--+--+
''')
label_art.config(font=('courier',70))
label_art.grid(row =1,pady=50)
l1= Label(f2,text='Select the Image in which \nyou want to hide text :')
l1.config(font=('courier',18))
l1.grid()
bws_button = Button(f2,text='Select',command=lambda : self.frame2_encode(f2))
bws_button.config(font=('courier',18))
bws_button.grid()
back_button = Button(f2, text='Cancel', command=lambda : Stegno.home(self,f2))
back_button.config(font=('courier',18))
back_button.grid(pady=15)
back_button.grid()
f2.grid()
def frame2_encode(self,f2):
ep= Frame(root)
myfile = tkinter.filedialog.askopenfilename(filetypes = ([('png', '*.png'),('jpeg', '*.jpeg'),('jpg', '*.jpg'),('All Files', '*.*')]))
if not myfile:
messagebox.showerror("Error","You have selected nothing !")
else:
myimg = Image.open(myfile)
myimage = myimg.resize((300,200))
img = ImageTk.PhotoImage(myimage)
l3= Label(ep,text='Selected Image')
l3.config(font=('courier',18))
l3.grid()
panel = Label(ep, image=img)
panel.image = img
self.output_image_size = os.stat(myfile)
self.o_image_w, self.o_image_h = myimg.size
panel.grid()
l2 = Label(ep, text='Enter the message')
l2.config(font=('courier',18))
l2.grid(pady=15)
text_area = Text(ep, width=50, height=10)
text_area.grid()
encode_button = Button(ep, text='Cancel', command=lambda : Stegno.home(self,ep))
encode_button.config(font=('courier',11))
data = text_area.get("1.0", "end-1c")
back_button = Button(ep, text='Encode', command=lambda : [self.enc_fun(text_area,myimg),Stegno.home(self,ep)])
back_button.config(font=('courier',11))
back_button.grid(pady=15)
encode_button.grid()
ep.grid(row=1)
f2.destroy()
def info(self):
try:
str = 'original image:-\nsize of original image:{}mb\nwidth: {}\nheight: {}\n\n' \
'decoded image:-\nsize of decoded image: {}mb\nwidth: {}' \
'\nheight: {}'.format(self.output_image_size.st_size/1000000,
self.o_image_w,self.o_image_h,
self.d_image_size/1000000,
self.d_image_w,self.d_image_h)
messagebox.showinfo('info',str)
except:
messagebox.showinfo('Info','Unable to get the information')
def genData(self,data):
newd = []
for i in data:
newd.append(format(ord(i), '08b'))
return newd
def modPix(self,pix, data):
datalist = self.genData(data)
lendata = len(datalist)
imdata = iter(pix)
for i in range(lendata):
pix = [value for value in imdata.__next__()[:3] +
imdata.__next__()[:3] +
imdata.__next__()[:3]]
for j in range(0, 8):
if (datalist[i][j] == '0') and (pix[j] % 2 != 0):
if (pix[j] % 2 != 0):
pix[j] -= 1
elif (datalist[i][j] == '1') and (pix[j] % 2 == 0):
pix[j] -= 1
if (i == lendata - 1):
if (pix[-1] % 2 == 0):
pix[-1] -= 1
else:
if (pix[-1] % 2 != 0):
pix[-1] -= 1
pix = tuple(pix)
yield pix[0:3]
yield pix[3:6]
yield pix[6:9]
def encode_enc(self,newimg, data):
w = newimg.size[0]
(x, y) = (0, 0)
for pixel in self.modPix(newimg.getdata(), data):
newimg.putpixel((x, y), pixel)
if (x == w - 1):
x = 0
y += 1
else:
x += 1
def enc_fun(self,text_area,myimg):
data = text_area.get("1.0", "end-1c")
if (len(data) == 0):
messagebox.showinfo("Alert","Kindly enter text in TextBox")
else:
newimg = myimg.copy()
self.encode_enc(newimg, data)
my_file = BytesIO()
temp=os.path.splitext(os.path.basename(myimg.filename))[0]
newimg.save(tkinter.filedialog.asksaveasfilename(initialfile=temp,filetypes = ([('png', '*.png')]),defaultextension=".png"))
self.d_image_size = my_file.tell()
self.d_image_w,self.d_image_h = newimg.size
messagebox.showinfo("Success","Encoding Successful\nFile is saved as Image_with_hiddentext.png in the same directory")
def page3(self,frame):
frame.destroy()
self.main(root)
root = Tk()
o = Stegno()
o.main(root)
root.mainloop()