Advertisement
Najeebsk

STEGANOGRAPHY.py

Mar 7th, 2024 (edited)
997
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.45 KB | None | 0 0
  1. import os
  2. from tkinter import Label
  3. import numpy as np
  4. from imageio import imread, imwrite
  5. import tkinter as tk
  6. from PIL import Image, ImageTk
  7. import imageio.v2 as imageio  # Use v2 to suppress deprecation warning
  8.  
  9. max_value = 255  # max uint value per pixel per channel
  10. header_len = 4 * 8  # uint32 bit length
  11.  
  12. def read_image(img_path):
  13.     """
  14.    Reads an image from file and flattens it.
  15.    Args:
  16.        img_path    path to the image
  17.    Returns:
  18.        ndarray     numpy array containing the image in a flat shape
  19.        ndarray     shape of the read image before flattening
  20.    """
  21.     img = np.array(imageio.imread(img_path), dtype=np.uint8)  # Use imageio.v2 to suppress deprecation warning
  22.     orig_shape = img.shape
  23.     return img.flatten(), orig_shape
  24.  
  25. def write_image(img_path, img_data, shape):
  26.     """
  27.    Writes an image to a path from a flat numpy array, using the shape provided.
  28.    Args:
  29.        img_path    path were to save the image
  30.        img_data    numpy array containing the image (flat)
  31.        shape       shape of the image to be saved
  32.    """
  33.     img_data = np.reshape(img_data, shape)
  34.     imwrite(img_path, img_data)
  35.  
  36. def bytes2array(byte_data):
  37.     """
  38.    Converts byte data to a bit array (numpy array, dtype=np.uint8).
  39.    Args:
  40.        byte_data   the byte data
  41.    Returns:
  42.        ndarray     a numpy array of the single bits that composed the byte data
  43.    """
  44.     byte_array = np.frombuffer(byte_data, dtype=np.uint8)
  45.     return np.unpackbits(byte_array)
  46.  
  47. def array2bytes(bit_array):
  48.     """
  49.    Converts a bit array (numpy array, dtype=np.uint8) to byte data.
  50.    Args:
  51.        bit_array   the bit array
  52.    Returns:
  53.        bytes       the byte data
  54.    """
  55.     byte_array = np.packbits(bit_array)
  56.     return byte_array.tobytes()
  57.  
  58. def read_file(file_path):
  59.     """
  60.    Reads a file as a bit array (numpy array, dtype=np.uint8)
  61.    Args:
  62.        file_path   path to the file
  63.    Returns:
  64.        ndarray     the bit array
  65.    """
  66.     file_bytes = open(file_path, "rb").read()
  67.     return bytes2array(file_bytes)
  68.  
  69. def write_file(file_path, file_bit_array):
  70.     """
  71.    Writes a file to a path from a bit array (numpy array, dtype=np.uint8).
  72.    Args:
  73.        file_path       path to the file
  74.        file_bit_array  the bit array of the file
  75.    """
  76.     bytes_data = array2bytes(file_bit_array)
  77.     with open(file_path, 'wb') as f:
  78.         f.write(bytes_data)
  79.  
  80. def encode_data(image, file_data):
  81.     """
  82.    Encodes the file data onto the image
  83.    Args:
  84.        image       the original image numpy array (flat)
  85.        file_data   the file data (bit array)
  86.    Returns:
  87.        ndarray     the encoded image as a numpy array
  88.    """
  89.     or_mask = file_data
  90.     and_mask = np.zeros_like(or_mask)
  91.     and_mask = (and_mask + max_value - 1) + or_mask
  92.     res = np.bitwise_or(image, or_mask)
  93.     res = np.bitwise_and(res, and_mask)
  94.     return res
  95.  
  96. def decode_data(encoded_data):
  97.     """
  98.    Decodes the data from an image
  99.    Args:
  100.        encoded_data    the encoded image as numpy array
  101.    Returns:
  102.        ndarray         the bit array containing the file bits
  103.    """
  104.     out_mask = np.ones_like(encoded_data)
  105.     output = np.bitwise_and(encoded_data, out_mask)
  106.     return output
  107.  
  108. def hide_images():
  109.     original_file = original_entry_hide.get()
  110.     hide_file = hide_entry_hide.get()
  111.     save_file = save_entry_hide.get()
  112.     img_path = f'DEEPFAKE/{original_file}.jpg'
  113.     file_path = f'DEEPFAKE/{hide_file}.jpg'
  114.     output_path = f'DATA/{save_file}.png'
  115.  
  116.     if not os.path.isfile(img_path):
  117.         print("Image file does not exist")
  118.         return
  119.     if not os.path.isfile(file_path):
  120.         print("File does not exist")
  121.         return
  122.  
  123.     image, shape_orig = read_image(img_path)
  124.     file = read_file(file_path)
  125.     file_len = file.shape[0]
  126.     len_array = np.array([file_len], dtype=np.uint32).view(np.uint8)
  127.     len_array = np.unpackbits(len_array)
  128.     img_len = image.shape[0]
  129.  
  130.     if file_len >= img_len - header_len:  # 4 bytes are used to store file length
  131.         print("File too big, error")
  132.         return
  133.     else:  #  Insert padding. Using random padding, otherwise values would all be even if padding with zeros (could be noticed in histogram).
  134.         tmp = file
  135.         file = np.random.randint(2, size=img_len, dtype=np.uint8)
  136.         file[header_len:header_len+file_len] = tmp
  137.         # file = np.pad(file, (header_len,img_len - file_len - header_len), 'constant', constant_values=(0, 0))
  138.  
  139.     file[:header_len] = len_array
  140.     encoded_data = encode_data(image, file)
  141.  
  142.     write_image(output_path, encoded_data, shape_orig)
  143.     print("Image encoded")
  144.  
  145.     # Preview encoded image
  146.     #preview_img = Image.fromarray(encoded_data.reshape(shape_orig))
  147.     #preview_img.show()
  148.  
  149. def unhide_images():
  150.     original_file = original_entry_unhide.get()
  151.     save_file = save_entry_unhide.get()
  152.     img_path = f'DATA/{original_file}.png'
  153.     if not os.path.isfile(img_path):
  154.         print("Image file does not exist")
  155.         return
  156.  
  157.     encoded_data, shape_orig = read_image(img_path)
  158.     data = decode_data(encoded_data)
  159.     el_array = np.packbits(data[:header_len])
  160.     extracted_len = el_array.view(np.uint32)[0]
  161.     data = data[header_len:header_len + extracted_len]
  162.  
  163.     # Ensure the reshaping operation matches the size of the array correctly
  164.     expected_rows = extracted_len // 3
  165.     if extracted_len % 3 != 0:
  166.         expected_rows += 1
  167.     data = np.pad(data, (0, 3 * expected_rows - extracted_len), 'constant')  # Pad to ensure correct reshaping
  168.  
  169.     write_file(f'DATA/{save_file}.jpg', data)
  170.     print("Image decoded")
  171.  
  172.     # Preview decoded image
  173.     #preview_img = Image.fromarray(data.reshape((expected_rows, 3)))
  174.     #preview_img.show()
  175.  
  176. root = tk.Tk()
  177. root.geometry("490x160")
  178. root.resizable(False, False)
  179. root.configure(bg="#34495e")
  180. root.title("Najeeb Generator AI")
  181.  
  182. #Label
  183. Label(root, text="NAJEEB AI IMAGES GENERATOR", bg="#34495e", fg="white", font="arial 22 bold").place(x=10, y=10)
  184.  
  185. # Hide Frame
  186. hide_frame = tk.Frame(root, bg="#FFFF99")
  187. hide_frame.grid(row=0, column=0, padx=10, pady=50)
  188.  
  189. tk.Label(hide_frame, text="Original Image:", bg="#FFFF99").grid(row=0, column=0)
  190. tk.Label(hide_frame, text="Bind Image:", bg="#FFFF99").grid(row=1, column=0)
  191. tk.Label(hide_frame, text="Save Image:", bg="#FFFF99").grid(row=2, column=0)
  192.  
  193. original_entry_hide = tk.Entry(hide_frame)
  194. hide_entry_hide = tk.Entry(hide_frame)
  195. save_entry_hide = tk.Entry(hide_frame)
  196.  
  197. original_entry_hide.grid(row=0, column=1)
  198. hide_entry_hide.grid(row=1, column=1)
  199. save_entry_hide.grid(row=2, column=1)
  200.  
  201. hide_button = tk.Button(hide_frame, text="MAKE-AI-BIND", command=hide_images, bg="#FF6666", fg="white")
  202. hide_button.grid(row=3, columnspan=2)
  203.  
  204. # Unhide Frame
  205. unhide_frame = tk.Frame(root, bg="#99FF99")
  206. unhide_frame.grid(row=0, column=1, padx=10, pady=50)
  207. tk.Label(unhide_frame, text="Both Side Enter Name Only:", bg="#99FF99").grid(row=0, column=1)
  208. tk.Label(unhide_frame, text="Bind AI Image:", bg="#99FF99").grid(row=1, column=0)
  209. tk.Label(unhide_frame, text="Save Encode:", bg="#99FF99").grid(row=2, column=0)
  210.  
  211. original_entry_unhide = tk.Entry(unhide_frame)
  212. save_entry_unhide = tk.Entry(unhide_frame)
  213.  
  214. original_entry_unhide.grid(row=1, column=1)
  215. save_entry_unhide.grid(row=2, column=1)
  216.  
  217. unhide_button = tk.Button(unhide_frame, text="GENERATE AI IMAGE", command=unhide_images, bg="#6699FF", fg="white")
  218. unhide_button.grid(row=3, columnspan=2)
  219.  
  220. root.mainloop()
  221.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement