-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstego.py
More file actions
259 lines (222 loc) · 9.32 KB
/
stego.py
File metadata and controls
259 lines (222 loc) · 9.32 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
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
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:
def main(self,root):
root.title('ImageSteganography')
root.geometry('500x600')
root.resizable(width =False, height=False)
f = Frame(root)
title = Label(f,text='INNOVATE 2K23 \n Image Steganography')
title.config(font=('Helvetica',33))
title.grid(pady=50)
b_encode = Button(f,text="Encode",command= lambda :self.frame1_encode(f),fg='blue',font='Helvetica',padx=14)
b_encode.config(font=('courier',14))
b_decode = Button(f, text="Decode",padx=14,command=lambda :self.frame1_decode(f),fg='blue',font='Helvetica')
b_decode.config(font=('courier',14))
b_decode.grid(pady = 20)
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)
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='INNOVATE 2K23')
label_art.config(font=('Helvetica',33))
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='INNOVATE 2K23')
label_art.config(font=('Helvetica',33))
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):
# Extracting 3 pixels at a time
pix = [value for value in imdata.__next__()[:3] +
imdata.__next__()[:3] +
imdata.__next__()[:3]]
# Pixel value should be made
# odd for 1 and even for 0
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
# Eigh^th pixel of every set tells
# whether to stop or read further.
# 0 means keep reading; 1 means the
# message is over.
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):
# Putting modified pixels in the new image
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()