-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageStenography.py
246 lines (217 loc) Β· 9.24 KB
/
ImageStenography.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
# DataFlair- import modules
from tkinter import *
import tkinter.filedialog
from tkinter import messagebox
from PIL import ImageTk
from PIL import Image
from io import BytesIO
import os
class IMG_Stegno:
output_image_size = 0
# DataFlair- main frame or start page
def main(self, root):
root.title('Image Stenography Program')
root.geometry('500x600')
root.resizable(width=False, height=False)
root.config(bg='#e3f4f1')
frame = Frame(root)
frame.grid()
title = Label(frame, text='DataFlair Image Steganography')
title.config(font=('Times new roman', 25, 'bold'))
title.grid(pady=10)
title.config(bg='#e3f4f1')
title.grid(row=1)
encode = Button(frame, text="Encode", command=lambda: self.encode_frame1(frame), padx=14, bg='#e3f4f1')
encode.config(font=('Helvetica', 14), bg='#e8c1c7')
encode.grid(row=2)
decode = Button(frame, text="Decode", command=lambda: self.decode_frame1(frame), padx=14, bg='#e3f4f1')
decode.config(font=('Helvetica', 14), bg='#e8c1c7')
decode.grid(pady=12)
decode.grid(row=3)
root.grid_rowconfigure(1, weight=1)
root.grid_columnconfigure(0, weight=1)
# DataFlair- back function to loop back to main screen
def back(self, frame):
frame.destroy()
self.main(root)
# DataFlair- frame for encode page
def encode_frame1(self, F):
F.destroy()
F2 = Frame(root)
label1 = Label(F2, text='Select the Image in which \nyou want to hide text :')
label1.config(font=('Times new roman', 25, 'bold'), bg='#e3f4f1')
label1.grid()
button_bws = Button(F2, text='Select', command=lambda: self.encode_frame2(F2))
button_bws.config(font=('Helvetica', 18), bg='#e8c1c7')
button_bws.grid()
button_back = Button(F2, text='Cancel', command=lambda: IMG_Stegno.back(self, F2))
button_back.config(font=('Helvetica', 18), bg='#e8c1c7')
button_back.grid(pady=15)
button_back.grid()
F2.grid()
# DataFlair- frame for decode page
def decode_frame1(self, F):
F.destroy()
d_f2 = Frame(root)
label1 = Label(d_f2, text='Select Image with Hidden text:')
label1.config(font=('Times new roman', 25, 'bold'), bg='#e3f4f1')
label1.grid()
label1.config(bg='#e3f4f1')
button_bws = Button(d_f2, text='Select', command=lambda: self.decode_frame2(d_f2))
button_bws.config(font=('Helvetica', 18), bg='#e8c1c7')
button_bws.grid()
button_back = Button(d_f2, text='Cancel', command=lambda: IMG_Stegno.back(self, d_f2))
button_back.config(font=('Helvetica', 18), bg='#e8c1c7')
button_back.grid(pady=15)
button_back.grid()
d_f2.grid()
# DataFlair- function to encode image
def encode_frame2(self, e_F2):
e_pg = 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:
my_img = Image.open(myfile)
new_image = my_img.resize((300, 200))
img = ImageTk.PhotoImage(new_image)
label3 = Label(e_pg, text='Selected Image')
label3.config(font=('Helvetica', 14, 'bold'))
label3.grid()
board = Label(e_pg, image=img)
board.image = img
self.output_image_size = os.stat(myfile)
self.o_image_w, self.o_image_h = my_img.size
board.grid()
label2 = Label(e_pg, text='Enter the message')
label2.config(font=('Helvetica', 14, 'bold'))
label2.grid(pady=15)
text_a = Text(e_pg, width=50, height=10)
text_a.grid()
encode_button = Button(e_pg, text='Cancel', command=lambda: IMG_Stegno.back(self, e_pg))
encode_button.config(font=('Helvetica', 14), bg='#e8c1c7')
data = text_a.get("1.0", "end-1c")
button_back = Button(e_pg, text='Encode',
command=lambda: [self.enc_fun(text_a, my_img), IMG_Stegno.back(self, e_pg)])
button_back.config(font=('Helvetica', 14), bg='#e8c1c7')
button_back.grid(pady=15)
encode_button.grid()
e_pg.grid(row=1)
e_F2.destroy()
# DataFlair- function to decode image
def decode_frame2(self, d_F2):
d_F3 = Frame(root)
myfiles = tkinter.filedialog.askopenfilename(
filetypes=([('png', '*.png'), ('jpeg', '*.jpeg'), ('jpg', '*.jpg'), ('All Files', '*.*')]))
if not myfiles:
messagebox.showerror("Error", "You have selected nothing! ")
else:
my_img = Image.open(myfiles, 'r')
my_image = my_img.resize((300, 200))
img = ImageTk.PhotoImage(my_image)
label4 = Label(d_F3, text='Selected Image :')
label4.config(font=('Helvetica', 14, 'bold'))
label4.grid()
board = Label(d_F3, image=img)
board.image = img
board.grid()
hidden_data = self.decode(my_img)
label2 = Label(d_F3, text='Hidden data is :')
label2.config(font=('Helvetica', 14, 'bold'))
label2.grid(pady=10)
text_a = Text(d_F3, width=50, height=10)
text_a.insert(INSERT, hidden_data)
text_a.configure(state='disabled')
text_a.grid()
button_back = Button(d_F3, text='Cancel', command=lambda: self.frame_3(d_F3))
button_back.config(font=('Helvetica', 14), bg='#e8c1c7')
button_back.grid(pady=15)
button_back.grid()
d_F3.grid(row=1)
d_F2.destroy()
# DataFair- function to decode data
def decode(self, image):
image_data = iter(image.getdata())
data = ''
while (True):
pixels = [value for value in image_data.__next__()[:3] +
image_data.__next__()[:3] +
image_data.__next__()[:3]]
binary_str = ''
for i in pixels[:8]:
if i % 2 == 0:
binary_str += '0'
else:
binary_str += '1'
data += chr(int(binary_str, 2))
if pixels[-1] % 2 != 0:
return data
# DataFlair- function to generate data
def generate_Data(self, data):
new_data = []
for i in data:
new_data.append(format(ord(i), '08b'))
return new_data
# DataFlair- function to modify the pixels of image
def modify_Pix(self, pix, data):
dataList = self.generate_Data(data)
dataLen = len(dataList)
imgData = iter(pix)
for i in range(dataLen):
# Extracting 3 pixels at a time
pix = [value for value in imgData.__next__()[:3] +
imgData.__next__()[:3] +
imgData.__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 == dataLen - 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]
# DataFlair- function to enter the data pixels in image
def encode_enc(self, newImg, data):
w = newImg.size[0]
(x, y) = (0, 0)
for pixel in self.modify_Pix(newImg.getdata(), data):
# Putting modified pixels in the new image
newImg.putpixel((x, y), pixel)
if (x == w - 1):
x = 0
y += 1
else:
x += 1
# DataFlair- function to enter hidden text
def enc_fun(self, text_a, myImg):
data = text_a.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 frame_3(self, frame):
frame.destroy()
self.main(root)
# GUI loop
root = Tk()
o = IMG_Stegno()
o.main(root)
root.mainloop()