Advertisement
Najeebsk

GENRATE-AI-IMAGE.pyw

Oct 10th, 2024
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.43 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() or 'hidden_image' not in globals():
  52.         print("No images loaded to zoom")
  53.         return
  54.    
  55.     zoom_value = zoom_slider.get() / 100  # Scale the value down to a fraction
  56.     resized_original = original_image.resize((int(original_image.width * zoom_value), int(original_image.height * zoom_value)))
  57.     resized_hidden = hidden_image.resize((int(hidden_image.width * zoom_value), int(hidden_image.height * zoom_value)))
  58.    
  59.     zoomed_original_photo = ImageTk.PhotoImage(resized_original)
  60.     zoomed_hidden_photo = ImageTk.PhotoImage(resized_hidden)
  61.  
  62.     original_image_label.config(image=zoomed_original_photo)
  63.     original_image_label.image = zoomed_original_photo
  64.     hidden_image_label.config(image=zoomed_hidden_photo)
  65.     hidden_image_label.image = zoomed_hidden_photo
  66.  
  67.     canvas.config(scrollregion=canvas.bbox(tk.ALL))
  68.  
  69.  
  70. def unhide_images():
  71.     global original_image, hidden_image  # Ensure these are global variables
  72.     original_file = original_entry_unhide.get()
  73.     save_file = save_file_entry_unhide.get()
  74.  
  75.     if not os.path.isfile(original_file):
  76.         print("Image file does not exist")
  77.         return
  78.  
  79.     # Read and decode the encoded image data
  80.     encoded_data, shape_orig = read_image(original_file)
  81.     data = decode_data(encoded_data)
  82.  
  83.     # Extract the length of the hidden file from the header
  84.     el_array = np.packbits(data[:header_len])
  85.     extracted_len = el_array.view(np.uint32)[0]
  86.    
  87.     # Extract the hidden file data based on the header length
  88.     data = data[header_len:extracted_len+header_len]
  89.    
  90.     # Write the hidden file to the specified save location (as .png)
  91.     save_file = save_file.replace('.jpg', '.png')  # Force saving as .png
  92.     write_file(save_file, data)
  93.    
  94.     print(f"File extracted and saved as {save_file}")
  95.  
  96.     # Load and display the original and unhidden images side by side
  97.     original_image = Image.open(original_file)
  98.     hidden_image = Image.open(save_file)
  99.    
  100.     resized_original = original_image.resize((200, 400))  # First-time view as 200x400
  101.     resized_hidden = hidden_image.resize((200, 400))  # First-time view as 200x400
  102.    
  103.     original_photo = ImageTk.PhotoImage(resized_original)
  104.     hidden_photo = ImageTk.PhotoImage(resized_hidden)
  105.  
  106.     # Update image labels and configure scroll region
  107.     original_image_label.config(image=original_photo)
  108.     original_image_label.image = original_photo
  109.     hidden_image_label.config(image=hidden_photo)
  110.     hidden_image_label.image = hidden_photo
  111.     canvas.config(scrollregion=canvas.bbox(tk.ALL))
  112.  
  113.     zoom_slider.set(30)  # Reset the zoom slider to default
  114.  
  115.  
  116. # Create the root window
  117. root = tk.Tk()
  118. root.geometry("1000x700")
  119. root.title("Najeeb AI Generator Image Tool")
  120. root.configure(bg="#282c34")
  121.  
  122. # Grid configuration
  123. root.grid_rowconfigure(2, weight=1)
  124. root.grid_columnconfigure(0, weight=1)
  125. root.grid_columnconfigure(1, weight=1)
  126. root.grid_columnconfigure(2, weight=4)  # Make column 2 wider for the canvas and zoom slider
  127.  
  128. # Create a frame for the left side (fields and buttons)
  129. left_frame = tk.Frame(root, bg="#282c34")
  130. left_frame.grid(row=0, column=0, padx=5, pady=5, sticky='nsew')
  131.  
  132. # Styling for labels and buttons
  133. label_font = ('Arial', 12, 'bold')
  134. entry_font = ('Arial', 11)
  135. button_font = ('Arial', 12, 'bold')
  136. button_color = "#61afef"
  137. button_fg = "white"
  138.  
  139. # Input for Encoded Image to unhide from
  140. tk.Label(left_frame, text="Select AI Image", font=label_font, bg="#282c34", fg="white").grid(row=0, column=0, sticky="e", padx=5)
  141. original_entry_unhide = tk.Entry(left_frame, width=20, font=entry_font)
  142. original_entry_unhide.grid(row=0, column=1, padx=5, pady=5)
  143. browse_button_unhide_image = tk.Button(left_frame, text="Browse", font=button_font, bg=button_color, fg=button_fg, command=lambda: original_entry_unhide.insert(0, filedialog.askopenfilename()))
  144. browse_button_unhide_image.grid(row=0, column=2, padx=5, pady=5)
  145.  
  146. # Input for Save Location of extracted file
  147. tk.Label(left_frame, text="Save AI(File.png):", font=label_font, bg="#282c34", fg="white").grid(row=0, column=3, sticky="e", padx=10)
  148. save_file_entry_unhide = tk.Entry(left_frame, width=20, font=entry_font)
  149. save_file_entry_unhide.grid(row=0, column=4, padx=5, pady=5)
  150. save_button_unhide_file = tk.Button(left_frame, text="Save As", font=button_font, bg=button_color, fg=button_fg, command=lambda: save_file_entry_unhide.insert(0, filedialog.asksaveasfilename()))
  151. save_button_unhide_file.grid(row=0, column=5, padx=10, pady=5)
  152.  
  153. # Button for decoding
  154. unhide_button = tk.Button(left_frame, text="Generate AI Image", font=button_font, bg="#98c379", fg="white", command=unhide_images)
  155. unhide_button.grid(row=0, column=6, columnspan=3, padx=10, pady=5)
  156.  
  157. # Create a canvas for image display with scrollbar
  158. canvas = tk.Canvas(root, bg="#000000")
  159. canvas.grid(row=2, column=0, columnspan=3, sticky="nsew")
  160.  
  161. # Add vertical and horizontal scrollbars
  162. vertical_scroll = tk.Scrollbar(root, orient="vertical", command=canvas.yview)
  163. vertical_scroll.grid(row=2, column=3, sticky="ns")
  164. canvas.configure(yscrollcommand=vertical_scroll.set)
  165.  
  166. horizontal_scroll = tk.Scrollbar(root, orient="horizontal", command=canvas.xview)
  167. horizontal_scroll.grid(row=3, column=0, columnspan=3, sticky="ew")
  168. canvas.configure(xscrollcommand=horizontal_scroll.set)
  169.  
  170. # Create image labels on the canvas for both images (original and hidden)
  171. original_image_label = tk.Label(canvas, bg="#000000")
  172. hidden_image_label = tk.Label(canvas, bg="#000000")
  173. canvas.create_window((0, 0), window=original_image_label, anchor='nw')
  174. canvas.create_window((220, 0), window=hidden_image_label, anchor='nw')  # Positioned next to the original image
  175.  
  176. # Zoom slider
  177. zoom_slider = tk.Scale(root, from_=10, to=200, bg="lightgreen", orient='horizontal', command=update_zoom)
  178. zoom_slider.set(100)  # Set default zoom level to 100%
  179. zoom_slider.grid(row=1, column=0, columnspan=3, sticky='ew', padx=10)
  180.  
  181. # Set initial scroll region
  182. canvas.config(scrollregion=canvas.bbox(tk.ALL))
  183.  
  184. # Run the application
  185. root.mainloop()
  186.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement