Advertisement
Najeebsk

GENRATE-AI-IMAGE2.0.pyw

Oct 10th, 2024
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.44 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 'unhidden_image' not in globals():
  52.         print("No image loaded to zoom")
  53.         return
  54.    
  55.     zoom_value = zoom_slider.get() / 100  # Scale the value down to a fraction
  56.    
  57.     # Resizing both images
  58.     resized_original = original_image.resize((int(original_image.width * zoom_value), int(original_image.height * zoom_value)))
  59.     zoomed_original_photo = ImageTk.PhotoImage(resized_original)
  60.    
  61.     resized_unhidden = unhidden_image.resize((int(unhidden_image.width * zoom_value), int(unhidden_image.height * zoom_value)))
  62.     zoomed_unhidden_photo = ImageTk.PhotoImage(resized_unhidden)
  63.  
  64.     # Update original image label
  65.     original_image_label.config(image=zoomed_original_photo)
  66.     original_image_label.image = zoomed_original_photo
  67.    
  68.     # Update unhidden image label
  69.     unhidden_image_label.config(image=zoomed_unhidden_photo)
  70.     unhidden_image_label.image = zoomed_unhidden_photo
  71.    
  72.     canvas.config(scrollregion=canvas.bbox(tk.ALL))
  73.  
  74. def unhide_images():
  75.     global original_image, unhidden_image  # Ensure these are global variables
  76.     original_file = original_entry_unhide.get()
  77.     save_file = save_file_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.     # Write the hidden file to the specified save location (as .png)
  95.     save_file = save_file.replace('.jpg', '.png')  # Force saving as .png
  96.     write_file(save_file, data)
  97.    
  98.     print(f"File extracted and saved as {save_file}")
  99.  
  100.     # Load and display both the original and unhidden images
  101.     original_image = Image.open(original_file)  # Load the original image
  102.     unhidden_image = Image.open(save_file)  # Load the unhidden image
  103.    
  104.     resized_original = original_image.resize((200, 400))  # First-time view as 200x400
  105.     original_photo = ImageTk.PhotoImage(resized_original)
  106.    
  107.     resized_unhidden = unhidden_image.resize((200, 400))  # First-time view as 200x400
  108.     unhidden_photo = ImageTk.PhotoImage(resized_unhidden)
  109.    
  110.     # Update labels and configure scroll region
  111.     original_image_label.config(image=original_photo)
  112.     original_image_label.image = original_photo
  113.    
  114.     unhidden_image_label.config(image=unhidden_photo)
  115.     unhidden_image_label.image = unhidden_photo
  116.    
  117.     canvas.config(scrollregion=canvas.bbox(tk.ALL))
  118.     zoom_slider.set(30)  # Reset the zoom slider to default
  119.  
  120. def save_images():
  121.     original_save_path = filedialog.asksaveasfilename(defaultextension=".png", filetypes=[("PNG files", "*.png")])
  122.     unhidden_save_path = filedialog.asksaveasfilename(defaultextension=".png", filetypes=[("PNG files", "*.png")])
  123.    
  124.     if original_save_path:
  125.         original_image.save(original_save_path)
  126.         print(f"Original image saved at {original_save_path}")
  127.    
  128.     if unhidden_save_path:
  129.         unhidden_image.save(unhidden_save_path)
  130.         print(f"Unhidden image saved at {unhidden_save_path}")
  131.  
  132. def screenshot_both_images():
  133.     if 'original_image' not in globals() or 'unhidden_image' not in globals():
  134.         print("No images to screenshot")
  135.         return
  136.  
  137.     # Create a new blank image to combine both images
  138.     combined_width = original_image.width + unhidden_image.width
  139.     combined_height = max(original_image.height, unhidden_image.height)
  140.     combined_image = Image.new('RGB', (combined_width, combined_height))
  141.    
  142.     # Paste original image and unhidden image into the combined image
  143.     combined_image.paste(original_image, (0, 0))
  144.     combined_image.paste(unhidden_image, (original_image.width, 0))
  145.  
  146.     # Save the combined image
  147.     save_path = filedialog.asksaveasfilename(defaultextension=".png", filetypes=[("PNG files", "*.png")])
  148.     if save_path:
  149.         combined_image.save(save_path)
  150.         print(f"Screenshot saved at {save_path}")        
  151.  
  152.  
  153. # Create the root window
  154. root = tk.Tk()
  155. root.geometry("1000x700")
  156. root.title("Najeeb AI Generator Image Tool")
  157. root.configure(bg="#282c34")
  158.  
  159. # Grid configuration
  160. root.grid_rowconfigure(2, weight=1)
  161. root.grid_columnconfigure(0, weight=1)
  162. root.grid_columnconfigure(1, weight=1)
  163. root.grid_columnconfigure(2, weight=4)  # Make column 2 wider for the canvas and zoom slider
  164.  
  165. # Create a frame for the left side (fields and buttons)
  166. left_frame = tk.Frame(root, bg="#282c34")
  167. left_frame.grid(row=0, column=0, padx=5, pady=5, sticky='nsew')
  168.  
  169. # Styling for labels and buttons
  170. label_font = ('Arial', 12, 'bold')
  171. entry_font = ('Arial', 11)
  172. button_font = ('Arial', 12, 'bold')
  173. button_color = "#61afef"
  174. button_fg = "white"
  175.  
  176. # Input for Encoded Image to unhide from
  177. tk.Label(left_frame, text="Select Image", font=label_font, bg="#282c34", fg="white").grid(row=0, column=0, sticky="e", padx=5)
  178. original_entry_unhide = tk.Entry(left_frame, width=12, font=entry_font)
  179. original_entry_unhide.grid(row=0, column=1, padx=5, pady=5)
  180. 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()))
  181. browse_button_unhide_image.grid(row=0, column=2, padx=5, pady=5)
  182.  
  183. # Input for Save Location of extracted file
  184. tk.Label(left_frame, text="Save AI:", font=label_font, bg="#282c34", fg="white").grid(row=0, column=3, sticky="e", padx=10)
  185. save_file_entry_unhide = tk.Entry(left_frame, width=12, font=entry_font)
  186. save_file_entry_unhide.grid(row=0, column=4, padx=5, pady=5)
  187. 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()))
  188. save_button_unhide_file.grid(row=0, column=5, padx=10, pady=5)
  189.  
  190. # Button for decoding
  191. unhide_button = tk.Button(left_frame, text="Generate AI", font=button_font, bg="#98c379", fg="white", command=unhide_images)
  192. unhide_button.grid(row=0, column=6, padx=10, pady=5)
  193.  
  194. # Button for taking a screenshot of both images
  195. screenshot_button = tk.Button(left_frame, text="Screenshot Both Images", font=button_font, bg=button_color, fg=button_fg, command=screenshot_both_images)
  196. screenshot_button.grid(row=0, column=7, padx=10, pady=5)
  197.  
  198. # Create a canvas for image display with scrollbar
  199. canvas = tk.Canvas(root, bg="#000000")
  200. canvas.grid(row=2, column=0, columnspan=3, sticky="nsew")
  201.  
  202. # Add vertical and horizontal scrollbars
  203. vertical_scroll = tk.Scrollbar(root, orient="vertical", command=canvas.yview)
  204. vertical_scroll.grid(row=2, column=3, sticky="ns")
  205. canvas.configure(yscrollcommand=vertical_scroll.set)
  206.  
  207. horizontal_scroll = tk.Scrollbar(root, orient="horizontal", command=canvas.xview)
  208. horizontal_scroll.grid(row=3, column=0, columnspan=3, sticky="ew")
  209. canvas.configure(xscrollcommand=horizontal_scroll.set)
  210.  
  211. # Frame to hold images inside canvas
  212. image_frame = tk.Frame(canvas, bg="#000000")
  213. canvas.create_window((0, 0), window=image_frame, anchor="nw")
  214.  
  215. # Labels to hold original and unhidden images
  216. original_image_label = tk.Label(image_frame)
  217. original_image_label.grid(row=0, column=0, padx=10, pady=10)
  218.  
  219. unhidden_image_label = tk.Label(image_frame)
  220. unhidden_image_label.grid(row=0, column=1, padx=10, pady=10)
  221.  
  222. # Zoom slider
  223. zoom_slider = tk.Scale(root, from_=10, to=200, orient="horizontal", command=update_zoom, font=label_font, bg="#282c34", fg="white")
  224. zoom_slider.grid(row=1, column=0, columnspan=3, sticky="ew", padx=10)
  225.  
  226. # Save button for both images
  227. #save_images_button = tk.Button(left_frame, text="Save Both Images", font=button_font, bg="#e06c75", fg="white", command=save_images)
  228. #save_images_button.grid(row=1, column=6, padx=10, pady=10)
  229.  
  230. # Start the main event loop
  231. root.mainloop()
  232.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement