Advertisement
Najeebsk

STEGANO-VIWE-IMAGE.py

Mar 8th, 2024 (edited)
506
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.00 KB | None | 0 0
  1. import os
  2. import numpy as np
  3. from imageio.v2 import imread, imwrite
  4. import tkinter as tk
  5. from PIL import Image, ImageTk
  6. import subprocess
  7.  
  8. max_value = 255  # max uint value per pixel per channel
  9. header_len = 4 * 8  # uint32 bit length
  10.  
  11. def read_image(img_path):
  12.     img = np.array(imread(img_path), dtype=np.uint8)
  13.     orig_shape = img.shape
  14.     return img.flatten(), orig_shape
  15.  
  16. def write_image(img_path, img_data, shape):
  17.     img_data = np.reshape(img_data, shape)
  18.     imwrite(img_path, img_data)
  19.  
  20. def bytes2array(byte_data):
  21.     byte_array = np.frombuffer(byte_data, dtype=np.uint8)
  22.     return np.unpackbits(byte_array)
  23.  
  24. def array2bytes(bit_array):
  25.     byte_array = np.packbits(bit_array)
  26.     return byte_array.tobytes()
  27.  
  28. def read_file(file_path):
  29.     file_bytes = open(file_path, "rb").read()
  30.     return bytes2array(file_bytes)
  31.  
  32. def write_file(file_path, file_bit_array):
  33.     bytes_data = array2bytes(file_bit_array)
  34.     with open(file_path, 'wb') as f:
  35.         f.write(bytes_data)
  36.  
  37. def encode_data(image, file_data):
  38.     or_mask = file_data
  39.     and_mask = np.zeros_like(or_mask)
  40.     and_mask = (and_mask + max_value - 1) + or_mask
  41.     res = np.bitwise_or(image, or_mask)
  42.     res = np.bitwise_and(res, and_mask)
  43.     return res
  44.  
  45. def decode_data(encoded_data):
  46.     out_mask = np.ones_like(encoded_data)
  47.     output = np.bitwise_and(encoded_data, out_mask)
  48.     return output
  49.  
  50. def hide_images():
  51.     original_file = original_entry_hide.get()
  52.     hide_file = hide_entry_hide.get()
  53.     save_file = save_entry_hide.get()
  54.     img_path = f'DEEPFAKE/{original_file}.jpg'
  55.     file_path = f'DEEPFAKE/{hide_file}.jpg'
  56.     output_path = f'DATA/{save_file}.png'
  57.  
  58.     if not os.path.isfile(img_path):
  59.         print("Image file does not exist")
  60.         return
  61.     if not os.path.isfile(file_path):
  62.         print("File does not exist")
  63.         return
  64.  
  65.     image, shape_orig = read_image(img_path)
  66.     file = read_file(file_path)
  67.     file_len = file.shape[0]
  68.     len_array = np.array([file_len], dtype=np.uint32).view(np.uint8)
  69.     len_array = np.unpackbits(len_array)
  70.     img_len = image.shape[0]
  71.  
  72.     if file_len >= img_len - header_len:
  73.         print("File too big, error")
  74.         return
  75.     else:
  76.         tmp = file
  77.         file = np.random.randint(2, size=img_len, dtype=np.uint8)
  78.         file[header_len:header_len+file_len] = tmp
  79.  
  80.     file[:header_len] = len_array
  81.     encoded_data = encode_data(image, file)
  82.  
  83.     write_image(output_path, encoded_data, shape_orig)
  84.     print("Image encoded")
  85.  
  86.     preview_img = Image.fromarray(encoded_data.reshape(shape_orig))
  87.     photo = ImageTk.PhotoImage(preview_img)
  88.     lbl.config(image=photo)
  89.     lbl.image = photo
  90.  
  91. def unhide_images():
  92.     original_file = original_entry_unhide.get()
  93.     save_file = save_entry_unhide.get()
  94.     img_path = f'DATA/{original_file}.png'
  95.     if not os.path.isfile(img_path):
  96.         print("Image file does not exist")
  97.         return
  98.  
  99.     encoded_data, shape_orig = read_image(img_path)
  100.     data = decode_data(encoded_data)
  101.     el_array = np.packbits(data[:header_len])
  102.     extracted_len = el_array.view(np.uint32)[0]
  103.     data = data[header_len:extracted_len+header_len]
  104.     write_file(f'DATA/{save_file}.jpg', data)
  105.     print("Image decoded and saved")
  106.  
  107.     saved_image_path = f'DATA/{save_file}.jpg'
  108.     #subprocess.Popen(['start', saved_image_path], shell=True)
  109.  
  110.     # Load the saved image for display in Frame2
  111.     saved_image = Image.open(saved_image_path)
  112.     #saved_image.thumbnail((shape_orig[0] // 3, shape_orig[1]))  # Resize if needed
  113.     photo = ImageTk.PhotoImage(saved_image)
  114.     lbl2.config(image=photo)
  115.     lbl2.image = photo
  116.  
  117.  
  118. root = tk.Tk()
  119. root.geometry("1000x660")
  120. root.title("Hide and Unhide Images")
  121.  
  122. # Frame for Original Image
  123. f = tk.Frame(root, bd=3, bg="#2c3e50", width=500, height=630, relief=tk.GROOVE)
  124. f.place(x=5, y=10)
  125.  
  126. tk.Label(f, text="Open Original Image:").place(x=10, y=10)
  127. original_entry_hide = tk.Entry(f)
  128. original_entry_hide.place(x=150, y=10)
  129.  
  130. tk.Label(f, text="Open Image to Hide:").place(x=10, y=40)
  131. hide_entry_hide = tk.Entry(f)
  132. hide_entry_hide.place(x=150, y=40)
  133.  
  134. tk.Label(f, text="Save Hidden Image:").place(x=10, y=70)
  135. save_entry_hide = tk.Entry(f)
  136. save_entry_hide.place(x=150, y=70)
  137.  
  138. hide_button = tk.Button(f, text="Hide", command=hide_images)
  139. hide_button.place(x=150, y=100)
  140.  
  141. lbl = tk.Label(f, bg="#2c3e50")
  142. lbl.place(x=10, y=140)
  143.  
  144. # Frame for Unhidden Image
  145. frame2 = tk.Frame(root, bd=3, width=500, height=630, relief=tk.GROOVE, bg="#34495e")
  146. frame2.place(x=505, y=10)
  147.  
  148. tk.Label(frame2, text="Open Encoded Image:").place(x=10, y=10)
  149. original_entry_unhide = tk.Entry(frame2)
  150. original_entry_unhide.place(x=150, y=10)
  151.  
  152. tk.Label(frame2, text="Save Unhidden Image:").place(x=10, y=40)
  153. save_entry_unhide = tk.Entry(frame2)
  154. save_entry_unhide.place(x=150, y=40)
  155.  
  156. unhide_button = tk.Button(frame2, text="Unhide", command=unhide_images)
  157. unhide_button.place(x=150, y=70)
  158.  
  159. lbl2 = tk.Label(frame2, bg="#34495e")
  160. lbl2.place(x=10, y=100)
  161.  
  162.  
  163. root.mainloop()
  164.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement