Advertisement
Najeebsk

AI-GENERATE4.0.pyw

Oct 7th, 2024
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.88 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. def hide_images():
  64.     global original_image  # Ensure this is a global variable
  65.     img_path = original_entry_hide.get()
  66.     file_path = hide_file_entry.get()
  67.     output_path = save_file_entry_hide.get()
  68.  
  69.     if not os.path.isfile(img_path):
  70.         print("Original image file does not exist")
  71.         return
  72.     if not os.path.isfile(file_path):
  73.         print("File to hide does not exist")
  74.         return
  75.  
  76.     image, shape_orig = read_image(img_path)
  77.     file = read_file(file_path)
  78.     file_len = file.shape[0]
  79.     len_array = np.array([file_len], dtype=np.uint32).view(np.uint8)
  80.     len_array = np.unpackbits(len_array)
  81.     img_len = image.shape[0]
  82.  
  83.     if file_len >= img_len - header_len:
  84.         print("File too big, error")
  85.         return
  86.     else:
  87.         tmp = file
  88.         file = np.random.randint(2, size=img_len, dtype=np.uint8)
  89.         file[header_len:header_len+file_len] = tmp
  90.  
  91.     file[:header_len] = len_array
  92.     encoded_data = encode_data(image, file)
  93.  
  94.     write_image(output_path, encoded_data, shape_orig)
  95.     print("Image encoded")
  96.  
  97.     # Update the preview image
  98.     original_image = Image.fromarray(encoded_data.reshape(shape_orig))
  99.     resized_image = original_image.resize((200, 400))  # First-time view as 200x400
  100.     photo = ImageTk.PhotoImage(resized_image)
  101.    
  102.     # Update label and configure scroll region
  103.     image_label.config(image=photo)
  104.     image_label.image = photo
  105.     canvas.config(scrollregion=canvas.bbox(tk.ALL))
  106.     zoom_slider.set(30)  # Reset the zoom slider to default
  107.  
  108. # Function to refresh the GUI
  109. def refresh_gui():
  110.     # Clear the entry fields
  111.     original_entry_hide.delete(0, tk.END)
  112.     hide_file_entry.delete(0, tk.END)
  113.     save_file_entry_hide.delete(0, tk.END)
  114.     original_entry_unhide.delete(0, tk.END)
  115.     save_file_entry_unhide.delete(0, tk.END)
  116.  
  117.     # Reset the zoom slider
  118.     zoom_slider.set(50)
  119.  
  120.     # Clear the image displayed on the canvas
  121.     image_label.config(image='')
  122.     canvas.config(scrollregion=canvas.bbox(tk.ALL))
  123.  
  124.     print("GUI refreshed")
  125.  
  126.  
  127. # Create the root window
  128. root = tk.Tk()
  129. root.geometry("1000x700")
  130. root.title("Najeeb AI Generator Image Tool")
  131. root.configure(bg="#282c34")
  132.  
  133. # Grid configuration
  134. root.grid_rowconfigure(2, weight=1)
  135. root.grid_columnconfigure(0, weight=1)
  136. root.grid_columnconfigure(1, weight=1)
  137. root.grid_columnconfigure(2, weight=4)  # Make column 2 wider for the canvas and zoom slider
  138.  
  139. # Create a frame for the left side (fields and buttons)
  140. left_frame = tk.Frame(root, bg="#282c34")
  141. left_frame.grid(row=0, column=0, padx=5, pady=5, sticky='nsew')
  142.  
  143. # Styling for labels and buttons
  144. label_font = ('Arial', 12, 'bold')
  145. entry_font = ('Arial', 11)
  146. button_font = ('Arial', 12, 'bold')
  147. button_color = "#61afef"
  148. button_fg = "white"
  149.  
  150. # Input for Original Image to hide file in
  151. tk.Label(left_frame, text="Select Original Image:", font=label_font, bg="#282c34", fg="white").grid(row=1, column=0, sticky="e", padx=10)
  152. original_entry_hide = tk.Entry(left_frame, width=20, font=entry_font)
  153. original_entry_hide.grid(row=1, column=1, padx=10, pady=5)
  154. browse_button_hide_image = tk.Button(left_frame, text="Browse", font=button_font, bg=button_color, fg=button_fg, command=lambda: original_entry_hide.insert(0, filedialog.askopenfilename()))
  155. browse_button_hide_image.grid(row=1, column=2, padx=10, pady=5)
  156.  
  157. # Input for File to hide
  158. tk.Label(left_frame, text="Select Convert AI Image", font=label_font, bg="#282c34", fg="white").grid(row=1, column=3, sticky="e", padx=10)
  159. hide_file_entry = tk.Entry(left_frame, width=20, font=entry_font)
  160. hide_file_entry.grid(row=1, column=4, padx=10, pady=5)
  161. browse_button_hide_file = tk.Button(left_frame, text="Browse", font=button_font, bg=button_color, fg=button_fg, command=lambda: hide_file_entry.insert(0, filedialog.askopenfilename()))
  162. browse_button_hide_file.grid(row=1, column=5, padx=10, pady=5)
  163.  
  164. # Input for Save Location of encoded image
  165. tk.Label(left_frame, text="Save Bind AI Image:", font=label_font, bg="#282c34", fg="white").grid(row=2, column=0, sticky="e", padx=10)
  166. save_file_entry_hide = tk.Entry(left_frame, width=20, font=entry_font)
  167. save_file_entry_hide.grid(row=2, column=1, padx=10, pady=5)
  168. save_button_hide_file = tk.Button(left_frame, text="Save As", font=button_font, bg=button_color, fg=button_fg, command=lambda: save_file_entry_hide.insert(0, filedialog.asksaveasfilename(defaultextension=".png")))
  169. save_button_hide_file.grid(row=2, column=2, padx=10, pady=5)
  170.  
  171. # Button for encoding
  172. hide_button = tk.Button(left_frame, text="AI Bind Image", font=button_font, bg="#98c379", fg="white", command=hide_images)
  173. hide_button.grid(row=2, column=3, columnspan=2, padx=10, pady=5)
  174.  
  175. # Add the refresh button to the left frame
  176. refresh_button = tk.Button(left_frame, text="Refresh", font=button_font, bg="#e06c75", fg="white", command=refresh_gui)
  177. refresh_button.grid(row=2, column=5, columnspan=2, padx=10, pady=5)
  178.  
  179. # Create a canvas for image display with scrollbar
  180. canvas = tk.Canvas(root, bg="#000000")
  181. canvas.grid(row=2, column=0, columnspan=3, sticky="nsew")
  182.  
  183. # Add vertical and horizontal scrollbars
  184. vertical_scroll = tk.Scrollbar(root, orient="vertical", command=canvas.yview)
  185. vertical_scroll.grid(row=2, column=3, sticky="ns")
  186. canvas.configure(yscrollcommand=vertical_scroll.set)
  187.  
  188. horizontal_scroll = tk.Scrollbar(root, orient="horizontal", command=canvas.xview)
  189. horizontal_scroll.grid(row=3, column=0, columnspan=3, sticky="ew")
  190. canvas.configure(xscrollcommand=horizontal_scroll.set)
  191.  
  192. # Create an image label on the canvas
  193. image_label = tk.Label(canvas, bg="#000000")
  194. canvas.create_window((0, 0), window=image_label, anchor='nw')
  195.  
  196. # Zoom slider
  197. zoom_slider = tk.Scale(root, from_=10, to=200, bg="lightgreen", orient='horizontal', command=update_zoom)
  198. zoom_slider.set(100)  # Set default zoom level to 100%
  199. zoom_slider.grid(row=1, column=0, columnspan=3, sticky='ew', padx=10)
  200.  
  201. # Set initial scroll region
  202. canvas.config(scrollregion=canvas.bbox(tk.ALL))
  203.  
  204. # Run the application
  205. root.mainloop()
  206.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement