└── Passing messages /Passing messages: -------------------------------------------------------------------------------- 1 | #import modules 2 | from tkinter import * 3 | import tkinter.filedialog 4 | from tkinter import messagebox 5 | from PIL import ImageTk 6 | from PIL import Image 7 | from io import BytesIO 8 | import os 9 | 10 | class IMG_Stegno: 11 | output_image_size = 0 12 | 13 | #main frame or start page 14 | def main(self, root): 15 | root.title('ImageSteganography by MKCE') 16 | root.geometry('500x600') 17 | root.resizable(width =False, height=False) 18 | root.config(bg = '#e3f4f1') 19 | frame = Frame(root) 20 | frame.grid() 21 | 22 | title = Label(frame,text='MKCE Image Steganography') 23 | title.config(font=('Times new roman',25, 'bold')) 24 | title.grid(pady=10) 25 | title.config(bg = '#e3f4f1') 26 | title.grid(row=1) 27 | 28 | encode = Button(frame,text="Encode",command= 29 | 30 | 31 | 32 | lambda :self.encode_frame1(frame), padx=14,bg = '#e3f4f1' ) 33 | encode.config(font=('Helvetica',14), bg='#e8c1c7') 34 | encode.grid(row=2) 35 | decode = Button(frame, 36 | text="Decode",command=lambda :self.decode_frame1(frame), padx=14,bg = 37 | '#e3f4f1') 38 | decode.config(font=('Helvetica',14), bg='#e8c1c7') 39 | decode.grid(pady = 12) 40 | decode.grid(row=3) 41 | 42 | root.grid_rowconfigure(1, weight=1) 43 | root.grid_columnconfigure(0, weight=1) 44 | 45 | #- back function to loop back to main screen 46 | def back(self,frame): 47 | frame.destroy() 48 | self.main(root) 49 | 50 | # frame for encode page 51 | def encode_frame1(self,F): 52 | F.destroy() 53 | F2 = Frame(root) 54 | label1= Label(F2,text='Select the Image in which \nyou want to hide text :') 55 | label1.config(font=('Times new roman',25, 'bold'),bg = '#e3f4f1') 56 | label1.grid() 57 | 58 | button_bws = Button(F2,text='Select',command=lambda : 59 | 60 | 61 | 62 | self.encode_frame2(F2)) 63 | 64 | button_bws.config(font=('Helvetica',18), bg='#e8c1c7') 65 | 66 | button_bws.grid() 67 | button_back = Button(F2, text='Cancel', command=lambda : 68 | IMG_Stegno.back(self,F2)) 69 | button_back.config(font=('Helvetica',18),bg='#e8c1c7') 70 | button_back.grid(pady=15) 71 | button_back.grid() 72 | F2.grid() 73 | 74 | #frame for decode page 75 | def decode_frame1(self,F): 76 | F.destroy() 77 | d_f2 = Frame(root) 78 | label1 = Label(d_f2, text='Select Image with Hidden text:') 79 | label1.config(font=('Times new roman',25,'bold'),bg = '#e3f4f1') 80 | label1.grid() 81 | label1.config(bg = '#e3f4f1') 82 | button_bws = Button(d_f2, text='Select', 83 | command=lambda :self.decode_frame2(d_f2)) 84 | button_bws.config(font=('Helvetica',18), bg='#e8c1c7') 85 | button_bws.grid() 86 | button_back = Button(d_f2, text='Cancel', command=lambda : 87 | IMG_Stegno.back(self,d_f2)) 88 | button_back.config(font=('Helvetica',18), bg='#e8c1c7') 89 | button_back.grid(pady=15) 90 | button_back.grid() 91 | d_f2.grid() 92 | 93 | 94 | 95 | #function to encode image 96 | def encode_frame2(self,e_F2): 97 | e_pg= Frame(root) 98 | 99 | myfile = tkinter.filedialog.askopenfilename(filetypes = ([('png', '*.png'),('jpeg', 100 | '*.jpeg'),('jpg', '*.jpg'),('All Files', '*.*')])) 101 | if not myfile: 102 | messagebox.showerror("Error","You have selected nothing !") 103 | else: 104 | my_img = Image.open(myfile) 105 | new_image = my_img.resize((300,200)) 106 | img = ImageTk.PhotoImage(new_image) 107 | label3= Label(e_pg,text='Selected Image') 108 | label3.config(font=('Helvetica',14,'bold')) 109 | label3.grid() 110 | board = Label(e_pg, image=img) 111 | board.image = img 112 | self.output_image_size = os.stat(myfile) 113 | self.o_image_w, self.o_image_h = my_img.size 114 | board.grid() 115 | label2 = Label(e_pg, text='Enter the message') 116 | label2.config(font=('Helvetica',14,'bold')) 117 | label2.grid(pady=15) 118 | text_a = Text(e_pg, width=50, height=10) 119 | text_a.grid() 120 | encode_button = Button(e_pg, text='Cancel', command=lambda : 121 | IMG_Stegno.back(self,e_pg)) 122 | encode_button.config(font=('Helvetica',14), bg='#e8c1c7') 123 | data = text_a.get("1.0", "end-1c") 124 | button_back = Button(e_pg, text='Encode', command=lambda : 125 | 126 | 127 | 128 | [self.enc_fun(text_a,my_img),IMG_Stegno.back(self,e_pg)]) 129 | button_back.config(font=('Helvetica',14), bg='#e8c1c7') 130 | button_back.grid(pady=15) 131 | encode_button.grid() 132 | e_pg.grid(row=1) 133 | 134 | e_F2.destroy() 135 | 136 | #function to decode image 137 | def decode_frame2(self,d_F2): 138 | d_F3 = Frame(root) 139 | myfiles = tkinter.filedialog.askopenfilename(filetypes = ([('png', '*.png'),('jpeg', 140 | '*.jpeg'),('jpg', '*.jpg'),('All Files', '*.*')])) 141 | if not myfiles: 142 | messagebox.showerror("Error","You have selected nothing! ") 143 | else: 144 | my_img = Image.open(myfiles, 'r') 145 | my_image = my_img.resize((300, 200)) 146 | img = ImageTk.PhotoImage(my_image) 147 | label4= Label(d_F3,text='Selected Image :') 148 | label4.config(font=('Helvetica',14,'bold')) 149 | label4.grid() 150 | board = Label(d_F3, image=img) 151 | board.image = img 152 | board.grid() 153 | hidden_data = self.decode(my_img) 154 | label2 = Label(d_F3, text='Hidden data is :') 155 | label2.config(font=('Helvetica',14,'bold')) 156 | label2.grid(pady=10) 157 | 158 | 159 | 160 | text_a = Text(d_F3, width=50, height=10) 161 | text_a.insert(INSERT, hidden_data) 162 | text_a.configure(state='disabled') 163 | text_a.grid() 164 | 165 | button_back = Button(d_F3, text='Cancel', command= 166 | lambda :self.frame_3(d_F3)) 167 | button_back.config(font=('Helvetica',14),bg='#e8c1c7') 168 | button_back.grid(pady=15) 169 | button_back.grid() 170 | d_F3.grid(row=1) 171 | d_F2.destroy() 172 | 173 | #function to decode data 174 | def decode(self, image): 175 | image_data = iter(image.getdata()) 176 | data = '' 177 | 178 | while (True): 179 | pixels = [value for value in image_data.__next__()[:3] + 180 | image_data.__next__()[:3] + 181 | image_data.__next__()[:3]] 182 | binary_str = '' 183 | for i in pixels[:8]: 184 | if i % 2 == 0: 185 | binary_str += '0' 186 | else: 187 | binary_str += '1' 188 | 189 | 190 | 191 | data += chr(int(binary_str, 2)) 192 | if pixels[-1] % 2 != 0: 193 | return data 194 | 195 | #function to generate data 196 | 197 | def generate_Data(self,data): 198 | new_data = [] 199 | 200 | for i in data: 201 | new_data.append(format(ord(i), '08b')) 202 | return new_data 203 | 204 | #function to modify the pixels of image 205 | def modify_Pix(self,pix, data): 206 | dataList = self.generate_Data(data) 207 | dataLen = len(dataList) 208 | imgData = iter(pix) 209 | for i in range(dataLen): 210 | # Extracting 3 pixels at a time 211 | pix = [value for value in imgData.__next__()[:3] + 212 | imgData.__next__()[:3] + 213 | imgData.__next__()[:3]] 214 | 215 | for j in range(0, 8): 216 | if (dataList[i][j] == '0') and (pix[j] % 2 != 0): 217 | if (pix[j] % 2 != 0): 218 | pix[j] -= 1 219 | 220 | 221 | 222 | elif (dataList[i][j] == '1') and (pix[j] % 2 == 0): 223 | pix[j] -= 1 224 | 225 | if (i == dataLen - 1): 226 | if (pix[-1] % 2 == 0): 227 | pix[-1] -= 1 228 | else: 229 | 230 | if (pix[-1] % 2 != 0): 231 | pix[-1] -= 1 232 | 233 | pix = tuple(pix) 234 | yield pix[0:3] 235 | yield pix[3:6] 236 | yield pix[6:9] 237 | 238 | #function to enter the data pixels in image 239 | def encode_enc(self,newImg, data): 240 | w = newImg.size[0] 241 | (x, y) = (0, 0) 242 | 243 | for pixel in self.modify_Pix(newImg.getdata(), data): 244 | 245 | # Putting modified pixels in the new image 246 | newImg.putpixel((x, y), pixel) 247 | if (x == w - 1): 248 | x = 0 249 | y += 1 250 | else: 251 | 252 | 253 | 254 | x += 1 255 | 256 | #function to enter hidden text 257 | def enc_fun(self,text_a,myImg): 258 | data = text_a.get("1.0", "end-1c") 259 | if (len(data) == 0): 260 | messagebox.showinfo("Alert","Kindly enter text in TextBox") 261 | else: 262 | 263 | newImg = myImg.copy() 264 | self.encode_enc(newImg, data) 265 | my_file = BytesIO() 266 | temp=os.path.splitext(os.path.basename(myImg.filename))[0] 267 | newImg.save(tkinter.filedialog.asksaveasfilename(initialfile=temp,filetypes = 268 | ([('png', '*.png')]),defaultextension=".png")) 269 | self.d_image_size = my_file.tell() 270 | self.d_image_w,self.d_image_h = newImg.size 271 | messagebox.showinfo("Success","Encoding Successful\nFile is saved as 272 | Image_with_hiddentext.png in the same directory") 273 | 274 | def frame_3(self,frame): 275 | frame.destroy() 276 | self.main(root) 277 | 278 | #GUI loop 279 | root = Tk() 280 | o = IMG_Stegno() 281 | o.main(root) 282 | 283 | 284 | 285 | root.mainloop() 286 | --------------------------------------------------------------------------------