Advertisement
Najeebsk

AI-GENERATE2.1.pyw

Sep 29th, 2024
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.53 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 tkinter import filedialog
  6. from PIL import Image, ImageTk
  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 update_zoom(event=None):
  51.     if 'original_image' not in globals():
  52.         print("No image loaded to zoom")
  53.         return
  54.    
  55.     # Update the displayed image based on the zoom scale value
  56.     zoom_value = zoom_slider.get() / 100  # Scale the value down to a fraction
  57.     resized_image = original_image.resize((int(original_image.width * zoom_value), int(original_image.height * zoom_value)))
  58.     zoomed_photo = ImageTk.PhotoImage(resized_image)
  59.     lbl.config(image=zoomed_photo)
  60.     lbl.image = zoomed_photo
  61.  
  62. def hide_images():
  63.     global original_image  # Ensure this is a global variable
  64.     img_path = original_entry_hide.get()
  65.     file_path = hide_file_entry.get()
  66.     output_path = save_file_entry_hide.get()
  67.  
  68.     if not os.path.isfile(img_path):
  69.         print("Original image file does not exist")
  70.         return
  71.     if not os.path.isfile(file_path):
  72.         print("File to hide does not exist")
  73.         return
  74.  
  75.     image, shape_orig = read_image(img_path)
  76.     file = read_file(file_path)
  77.     file_len = file.shape[0]
  78.     len_array = np.array([file_len], dtype=np.uint32).view(np.uint8)
  79.     len_array = np.unpackbits(len_array)
  80.     img_len = image.shape[0]
  81.  
  82.     if file_len >= img_len - header_len:
  83.         print("File too big, error")
  84.         return
  85.     else:
  86.         tmp = file
  87.         file = np.random.randint(2, size=img_len, dtype=np.uint8)
  88.         file[header_len:header_len+file_len] = tmp
  89.  
  90.     file[:header_len] = len_array
  91.     encoded_data = encode_data(image, file)
  92.  
  93.     write_image(output_path, encoded_data, shape_orig)
  94.     print("Image encoded")
  95.  
  96.     # Update the preview image
  97.     original_image = Image.fromarray(encoded_data.reshape(shape_orig))
  98.     photo = ImageTk.PhotoImage(original_image)
  99.     lbl.config(image=photo)
  100.     lbl.image = photo
  101.     zoom_slider.set(100)  # Reset the zoom slider to default
  102.  
  103. def unhide_images():
  104.     global original_image  # Ensure this is a global variable
  105.     original_file = original_entry_unhide.get()
  106.     save_file = save_file_entry_unhide.get()
  107.  
  108.     if not os.path.isfile(original_file):
  109.         print("Image file does not exist")
  110.         return
  111.  
  112.     encoded_data, shape_orig = read_image(original_file)
  113.     data = decode_data(encoded_data)
  114.     el_array = np.packbits(data[:header_len])
  115.     extracted_len = el_array.view(np.uint32)[0]
  116.     data = data[header_len:extracted_len+header_len]
  117.     write_file(save_file, data)
  118.     print("File extracted and saved")
  119.  
  120.     # Load and display the original image
  121.     original_image = Image.open(original_file)
  122.     original_image.thumbnail((shape_orig[1], shape_orig[0]))  # Resize if needed
  123.     original_photo = ImageTk.PhotoImage(original_image)
  124.     lbl.config(image=original_photo)
  125.     lbl.image = original_photo
  126.     zoom_slider.set(100)  # Reset the zoom slider to default
  127.  
  128. # Create the root window
  129. root = tk.Tk()
  130. root.geometry("1000x700")
  131. root.title("Image Steganography Tool")
  132. root.configure(bg="#282c34")
  133.  
  134. # Styling for labels and buttons
  135. label_font = ('Arial', 12, 'bold')
  136. entry_font = ('Arial', 11)
  137. button_font = ('Arial', 12, 'bold')
  138. button_color = "#61afef"
  139. button_fg = "white"
  140.  
  141. # Hiding Section
  142. tk.Label(root, text="Hide a File Inside an Image", font=('Arial', 14, 'bold'), bg="#282c34", fg="white").grid(row=0, column=0, columnspan=2, pady=20)
  143.  
  144. # Input for Original Image to hide file in
  145. tk.Label(root, text="Select Original Image:", font=label_font, bg="#282c34", fg="white").grid(row=1, column=0, sticky="e", padx=10)
  146. original_entry_hide = tk.Entry(root, width=40, font=entry_font)
  147. original_entry_hide.grid(row=1, column=1, padx=10, pady=5)
  148. browse_button_hide_image = tk.Button(root, text="Browse", font=button_font, bg=button_color, fg=button_fg, command=lambda: original_entry_hide.insert(0, filedialog.askopenfilename()))
  149. browse_button_hide_image.grid(row=1, column=2, padx=10, pady=5)
  150.  
  151. # Input for File to hide
  152. tk.Label(root, text="Select File to Hide:", font=label_font, bg="#282c34", fg="white").grid(row=2, column=0, sticky="e", padx=10)
  153. hide_file_entry = tk.Entry(root, width=40, font=entry_font)
  154. hide_file_entry.grid(row=2, column=1, padx=10, pady=5)
  155. browse_button_hide_file = tk.Button(root, text="Browse", font=button_font, bg=button_color, fg=button_fg, command=lambda: hide_file_entry.insert(0, filedialog.askopenfilename()))
  156. browse_button_hide_file.grid(row=2, column=2, padx=10, pady=5)
  157.  
  158. # Input for Save Location of encoded image
  159. tk.Label(root, text="Save Hidden Image As:", font=label_font, bg="#282c34", fg="white").grid(row=3, column=0, sticky="e", padx=10)
  160. save_file_entry_hide = tk.Entry(root, width=40, font=entry_font)
  161. save_file_entry_hide.grid(row=3, column=1, padx=10, pady=5)
  162. browse_button_save_hide = tk.Button(root, text="Browse", font=button_font, bg=button_color, fg=button_fg, command=lambda: save_file_entry_hide.insert(0, filedialog.asksaveasfilename(defaultextension=".png")))
  163. browse_button_save_hide.grid(row=3, column=2, padx=10, pady=5)
  164.  
  165. # Button to hide the file
  166. hide_button = tk.Button(root, text="Hide and Save Image", font=button_font, bg="#98c379", fg="black", command=hide_images)
  167. hide_button.grid(row=4, column=1, pady=20)
  168.  
  169. # Unhiding Section
  170. tk.Label(root, text="Unhide File from Image", font=('Arial', 14, 'bold'), bg="#282c34", fg="white").grid(row=5, column=0, columnspan=2, pady=20)
  171.  
  172. # Input for Encoded Image to unhide file from
  173. tk.Label(root, text="Select Encoded Image:", font=label_font, bg="#282c34", fg="white").grid(row=6, column=0, sticky="e", padx=10)
  174. original_entry_unhide = tk.Entry(root, width=40, font=entry_font)
  175. original_entry_unhide.grid(row=6, column=1, padx=10, pady=5)
  176. browse_button_unhide_image = tk.Button(root, text="Browse", font=button_font, bg=button_color, fg=button_fg, command=lambda: original_entry_unhide.insert(0, filedialog.askopenfilename()))
  177. browse_button_unhide_image.grid(row=6, column=2, padx=10, pady=5)
  178.  
  179. # Input for Save Location of extracted file
  180. tk.Label(root, text="Save Extracted File As:", font=label_font, bg="#282c34", fg="white").grid(row=7, column=0, sticky="e", padx=10)
  181. save_file_entry_unhide = tk.Entry(root, width=40, font=entry_font)
  182. save_file_entry_unhide.grid(row=7, column=1, padx=10, pady=5)
  183. browse_button_save_unhide = tk.Button(root, text="Browse", font=button_font, bg=button_color, fg=button_fg, command=lambda: save_file_entry_unhide.insert(0, filedialog.asksaveasfilename()))
  184. browse_button_save_unhide.grid(row=7, column=2, padx=10, pady=5)
  185.  
  186. # Button to unhide the file
  187. unhide_button = tk.Button(root, text="Extract and Save File", font=button_font, bg="#e06c75", fg="black", command=unhide_images)
  188. unhide_button.grid(row=8, column=1, pady=20)
  189.  
  190. # Preview Section for Original and Encoded Images
  191. preview_frame = tk.Frame(root, relief="groove", bg="white")
  192. preview_frame.grid(row=0, column=3, rowspan=10, padx=20, pady=10)
  193. lbl = tk.Label(preview_frame, text="Preview", bg="white", font=label_font)
  194. lbl.grid(row=0, column=0)
  195.  
  196. # Image Zoom Slider
  197. zoom_slider = tk.Scale(root, from_=10, to=200, orient='horizontal', label="Zoom Image (%)", font=label_font, bg="#282c34", fg="white", command=update_zoom)
  198. zoom_slider.grid(row=9, column=3, padx=20, pady=10)
  199. zoom_slider.set(100)  # Default zoom is 100%
  200.  
  201. root.mainloop()
  202.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement