Advertisement
Najeebsk

GENRATE-AI-IMAGE1.0.pyw

Oct 10th, 2024
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.18 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.     with open(file_path, "rb") as f:
  30.         file_bytes = f.read()
  31.     return bytes2array(file_bytes)
  32.  
  33. def write_file(file_path, file_bit_array):
  34.     bytes_data = array2bytes(file_bit_array)
  35.     with open(file_path, 'wb') as f:
  36.         f.write(bytes_data)
  37.  
  38. def encode_data(image, file_data):
  39.     or_mask = file_data
  40.     and_mask = np.zeros_like(or_mask)
  41.     and_mask = (and_mask + max_value - 1) + or_mask
  42.     res = np.bitwise_or(image, or_mask)
  43.     res = np.bitwise_and(res, and_mask)
  44.     return res
  45.  
  46. def decode_data(encoded_data):
  47.     out_mask = np.ones_like(encoded_data)
  48.     output = np.bitwise_and(encoded_data, out_mask)
  49.     return output
  50.  
  51. def update_zoom(event=None):
  52.     if 'original_image' not in globals() or 'unhidden_image' not in globals():
  53.         print("No image loaded to zoom")
  54.         return
  55.    
  56.     zoom_value = zoom_slider.get() / 100  # Scale the value down to a fraction
  57.    
  58.     # Resizing both images
  59.     resized_original = original_image.resize((int(original_image.width * zoom_value), int(original_image.height * zoom_value)))
  60.     zoomed_original_photo = ImageTk.PhotoImage(resized_original)
  61.    
  62.     resized_unhidden = unhidden_image.resize((int(unhidden_image.width * zoom_value), int(unhidden_image.height * zoom_value)))
  63.     zoomed_unhidden_photo = ImageTk.PhotoImage(resized_unhidden)
  64.  
  65.     # Update original image label
  66.     original_image_label.config(image=zoomed_original_photo)
  67.     original_image_label.image = zoomed_original_photo
  68.    
  69.     # Update unhidden image label
  70.     unhidden_image_label.config(image=zoomed_unhidden_photo)
  71.     unhidden_image_label.image = zoomed_unhidden_photo
  72.    
  73.     canvas.config(scrollregion=canvas.bbox(tk.ALL))
  74.  
  75. def unhide_images():
  76.     global original_image, unhidden_image  # Ensure these are global variables
  77.     original_file = original_entry_unhide.get()
  78.    
  79.     if not os.path.isfile(original_file):
  80.         print("Image file does not exist")
  81.         return
  82.  
  83.     # Read and decode the encoded image data
  84.     encoded_data, shape_orig = read_image(original_file)
  85.     data = decode_data(encoded_data)
  86.  
  87.     # Extract the length of the hidden file from the header
  88.     el_array = np.packbits(data[:header_len])
  89.     extracted_len = el_array.view(np.uint32)[0]
  90.    
  91.     # Extract the hidden file data based on the header length
  92.     data = data[header_len:extracted_len + header_len]
  93.  
  94.     # Automatically determine the save path for the extracted file
  95.     save_file = original_file.replace('.jpg', '_Generate_AI.png').replace('.png', '_Generate_AI.png')
  96.    
  97.     # Write the hidden file to the specified save location (as .png)
  98.     write_file(save_file, data)
  99.     print(f"File extracted and saved as {save_file}")
  100.  
  101.     # Load and display both the original and unhidden images
  102.     original_image = Image.open(original_file)  # Load the original image
  103.     unhidden_image = Image.open(save_file)  # Load the unhidden image
  104.    
  105.     update_image_display()
  106.  
  107. def update_image_display():
  108.     global original_image, unhidden_image
  109.     resized_original = original_image.resize((200, 400))  # First-time view as 200x400
  110.     original_photo = ImageTk.PhotoImage(resized_original)
  111.    
  112.     resized_unhidden = unhidden_image.resize((200, 400))  # First-time view as 200x400
  113.     unhidden_photo = ImageTk.PhotoImage(resized_unhidden)
  114.    
  115.     # Update labels and configure scroll region
  116.     original_image_label.config(image=original_photo)
  117.     original_image_label.image = original_photo
  118.    
  119.     unhidden_image_label.config(image=unhidden_photo)
  120.     unhidden_image_label.image = unhidden_photo
  121.    
  122.     canvas.config(scrollregion=canvas.bbox(tk.ALL))
  123.     zoom_slider.set(100)  # Reset the zoom slider to default
  124.  
  125. def screenshot_both_images():
  126.     if 'original_image' not in globals() or 'unhidden_image' not in globals():
  127.         print("No images to screenshot")
  128.         return
  129.  
  130.     # Create a new blank image to combine both images
  131.     combined_width = original_image.width + unhidden_image.width
  132.     combined_height = max(original_image.height, unhidden_image.height)
  133.     combined_image = Image.new('RGB', (combined_width, combined_height))
  134.    
  135.     # Paste original image and unhidden image into the combined image
  136.     combined_image.paste(original_image, (0, 0))
  137.     combined_image.paste(unhidden_image, (original_image.width, 0))
  138.  
  139.     # Save the combined image
  140.     save_path = filedialog.asksaveasfilename(defaultextension=".png", filetypes=[("PNG files", "*.png")])
  141.     if save_path:
  142.         combined_image.save(save_path)
  143.         print(f"Screenshot saved at {save_path}")
  144.  
  145. # Create the root window
  146. root = tk.Tk()
  147. root.geometry("1000x700")
  148. root.title("Najeeb AI Generator Image Tool")
  149. root.configure(bg="#282c34")
  150.  
  151. # Grid configuration
  152. root.grid_rowconfigure(2, weight=1)
  153. root.grid_columnconfigure(0, weight=1)
  154. root.grid_columnconfigure(1, weight=1)
  155. root.grid_columnconfigure(2, weight=4)  # Make column 2 wider for the canvas and zoom slider
  156.  
  157. # Create a frame for the left side (fields and buttons)
  158. left_frame = tk.Frame(root, bg="#282c34")
  159. left_frame.grid(row=0, column=0, padx=5, pady=5, sticky='nsew')
  160.  
  161. # Styling for labels and buttons
  162. label_font = ('Arial', 12, 'bold')
  163. entry_font = ('Arial', 11)
  164. button_font = ('Arial', 12, 'bold')
  165. button_color = "#61afef"
  166. button_fg = "white"
  167.  
  168. # Input for Encoded Image to unhide from
  169. tk.Label(left_frame, text="Select AI Image", font=label_font, bg="#282c34", fg="white").grid(row=0, column=0, sticky="e", padx=5)
  170. original_entry_unhide = tk.Entry(left_frame, width=20, font=entry_font)
  171. original_entry_unhide.grid(row=0, column=1, padx=5, pady=5)
  172. 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()))
  173. browse_button_unhide_image.grid(row=0, column=2, padx=5, pady=5)
  174.  
  175. # Button for decoding
  176. unhide_button = tk.Button(left_frame, text="Generate AI Image", font=button_font, bg="#98c379", fg="white", command=unhide_images)
  177. unhide_button.grid(row=0, column=3, columnspan=3, padx=10, pady=5)
  178.  
  179. # Button for taking a screenshot of both images
  180. screenshot_button = tk.Button(left_frame, text="Screenshot Both Images", font=button_font, bg=button_color, fg=button_fg, command=screenshot_both_images)
  181. screenshot_button.grid(row=0, column=6, columnspan=3, padx=10, pady=5)
  182.  
  183. # Create a frame for the canvas and scrollbar
  184. canvas_frame = tk.Frame(root)
  185. canvas_frame.grid(row=2, column=0, columnspan=3, sticky='nsew')
  186.  
  187. # Create a canvas
  188. canvas = tk.Canvas(canvas_frame, bg="black")
  189. canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
  190.  
  191. # Add a scrollbar
  192. scrollbar = tk.Scrollbar(canvas_frame, command=canvas.yview)
  193. scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
  194.  
  195. canvas.config(yscrollcommand=scrollbar.set)
  196.  
  197. # Create a frame inside the canvas
  198. image_frame = tk.Frame(canvas, bg="black")
  199. canvas.create_window((0, 0), window=image_frame, anchor='nw')
  200.  
  201. # Labels for the original and unhidden images
  202. original_image_label = tk.Label(image_frame, bg="black")
  203. original_image_label.grid(row=0, column=0, padx=5, pady=5)
  204.  
  205. unhidden_image_label = tk.Label(image_frame, bg="black")
  206. unhidden_image_label.grid(row=0, column=1, padx=5, pady=5)
  207.  
  208. # Zoom slider
  209. zoom_slider = tk.Scale(left_frame, from_=10, to=300, orient="horizontal", label="Zoom (%)", command=update_zoom)
  210. zoom_slider.set(100)  # Set default zoom value
  211. zoom_slider.grid(row=1, column=0, columnspan=3, padx=5, pady=5)
  212.  
  213. root.mainloop()
  214.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement