Advertisement
Najeebsk

AI-GENERATE5.0.pyw

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