Advertisement
Najeebsk

AI-GENRATER6.0.pyw

Sep 30th, 2024 (edited)
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 11.08 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.     # Update the displayed image based on the zoom scale value
  56.     zoom_value = zoom_slider.get() / 100  # Scale the value down to a fraction
  57.     resized_image = original_image.resize((int(original_image.width * zoom_value), int(original_image.height * zoom_value)))
  58.     zoomed_photo = ImageTk.PhotoImage(resized_image)
  59.  
  60.     # Configure scroll region and update image
  61.     image_label.config(image=zoomed_photo)
  62.     image_label.image = zoomed_photo
  63.     canvas.config(scrollregion=canvas.bbox(tk.ALL))
  64.  
  65. def hide_images():
  66.     global original_image  # Ensure this is a global variable
  67.     img_path = original_entry_hide.get()
  68.     file_path = hide_file_entry.get()
  69.     output_path = save_file_entry_hide.get()
  70.  
  71.     if not os.path.isfile(img_path):
  72.         print("Original image file does not exist")
  73.         return
  74.     if not os.path.isfile(file_path):
  75.         print("File to hide does not exist")
  76.         return
  77.  
  78.     image, shape_orig = read_image(img_path)
  79.     file = read_file(file_path)
  80.     file_len = file.shape[0]
  81.     len_array = np.array([file_len], dtype=np.uint32).view(np.uint8)
  82.     len_array = np.unpackbits(len_array)
  83.     img_len = image.shape[0]
  84.  
  85.     if file_len >= img_len - header_len:
  86.         print("File too big, error")
  87.         return
  88.     else:
  89.         tmp = file
  90.         file = np.random.randint(2, size=img_len, dtype=np.uint8)
  91.         file[header_len:header_len+file_len] = tmp
  92.  
  93.     file[:header_len] = len_array
  94.     encoded_data = encode_data(image, file)
  95.  
  96.     write_image(output_path, encoded_data, shape_orig)
  97.     print("Image encoded")
  98.  
  99.     # Update the preview image
  100.     original_image = Image.fromarray(encoded_data.reshape(shape_orig))
  101.     resized_image = original_image.resize((200, 400))  # First-time view as 200x400
  102.     photo = ImageTk.PhotoImage(resized_image)
  103.    
  104.     # Update label and configure scroll region
  105.     image_label.config(image=photo)
  106.     image_label.image = photo
  107.     canvas.config(scrollregion=canvas.bbox(tk.ALL))
  108.     zoom_slider.set(30)  # Reset the zoom slider to default
  109.  
  110. def unhide_images():
  111.     global original_image  # Ensure this is a global variable
  112.     original_file = original_entry_unhide.get()
  113.     save_file = save_file_entry_unhide.get()
  114.  
  115.     if not os.path.isfile(original_file):
  116.         print("Image file does not exist")
  117.         return
  118.  
  119.     # Read and decode the encoded image data
  120.     encoded_data, shape_orig = read_image(original_file)
  121.     data = decode_data(encoded_data)
  122.  
  123.     # Extract the length of the hidden file from the header
  124.     el_array = np.packbits(data[:header_len])
  125.     extracted_len = el_array.view(np.uint32)[0]
  126.    
  127.     # Extract the hidden file data based on the header length
  128.     data = data[header_len:extracted_len+header_len]
  129.    
  130.     # Write the hidden file to the specified save location (as .jpg)
  131.     save_file = save_file.replace('.jpg', '.png')  # Force saving as .jpg
  132.     write_file(save_file, data)
  133.    
  134.     print(f"File extracted and saved as {save_file}")
  135.  
  136.  
  137.     # Load and display the original image
  138.     original_image = Image.open(save_file)
  139.     resized_image = original_image.resize((200, 400))  # First-time view as 200x400
  140.     photo = ImageTk.PhotoImage(resized_image)
  141.    
  142.     # Update label and configure scroll region
  143.     image_label.config(image=photo)
  144.     image_label.image = photo
  145.     canvas.config(scrollregion=canvas.bbox(tk.ALL))
  146.     zoom_slider.set(30)  # Reset the zoom slider to default
  147.  
  148. # Function to refresh the GUI
  149. def refresh_gui():
  150.     # Clear the entry fields
  151.     original_entry_hide.delete(0, tk.END)
  152.     hide_file_entry.delete(0, tk.END)
  153.     save_file_entry_hide.delete(0, tk.END)
  154.     original_entry_unhide.delete(0, tk.END)
  155.     save_file_entry_unhide.delete(0, tk.END)
  156.  
  157.     # Reset the zoom slider
  158.     zoom_slider.set(50)
  159.  
  160.     # Clear the image displayed on the canvas
  161.     image_label.config(image='')
  162.     canvas.config(scrollregion=canvas.bbox(tk.ALL))
  163.  
  164.     print("GUI refreshed")    
  165.  
  166. # Create the root window
  167. root = tk.Tk()
  168. root.geometry("1000x700")
  169. root.title("Najeeb AI Generator Image Tool")
  170. root.configure(bg="#282c34")
  171.  
  172. # Grid configuration
  173. root.grid_columnconfigure(0, weight=1)
  174. root.grid_columnconfigure(1, weight=1)
  175. root.grid_columnconfigure(2, weight=4)  # Make column 2 wider for the canvas and zoom slider
  176.  
  177. # Create a frame for the left side (text headings, fields, and buttons)
  178. left_frame = tk.Frame(root, bg="#282c34")
  179. left_frame.grid(row=0, column=0, rowspan=10, padx=10, pady=10, sticky='nsew')
  180.  
  181. # Styling for labels and buttons
  182. label_font = ('Arial', 12, 'bold')
  183. entry_font = ('Arial', 11)
  184. button_font = ('Arial', 12, 'bold')
  185. button_color = "#61afef"
  186. button_fg = "white"
  187.  
  188. # Hiding Section
  189. tk.Label(left_frame, text="Najeeb AI Image Generator", font=('Arial', 14, 'bold'), bg="#282c34", fg="white").grid(row=0, column=0, columnspan=2, pady=20)
  190.  
  191. # Input for Original Image to hide file in
  192. tk.Label(left_frame, text="Select Original Image:", font=label_font, bg="#282c34", fg="white").grid(row=1, column=0, sticky="e", padx=10)
  193. original_entry_hide = tk.Entry(left_frame, width=20, font=entry_font)
  194. original_entry_hide.grid(row=2, column=0, padx=10, pady=5)
  195. 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()))
  196. browse_button_hide_image.grid(row=2, column=1, padx=10, pady=5)
  197.  
  198. # Input for File to hide
  199. tk.Label(left_frame, text="Select Convert AI Image", font=label_font, bg="#282c34", fg="white").grid(row=3, column=0, sticky="e", padx=10)
  200. hide_file_entry = tk.Entry(left_frame, width=20, font=entry_font)
  201. hide_file_entry.grid(row=4, column=0, padx=10, pady=5)
  202. 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()))
  203. browse_button_hide_file.grid(row=4, column=1, padx=10, pady=5)
  204.  
  205. # Input for Save Location of encoded image
  206. tk.Label(left_frame, text="Save Bind AI Image:", font=label_font, bg="#282c34", fg="white").grid(row=5, column=0, sticky="e", padx=10)
  207. save_file_entry_hide = tk.Entry(left_frame, width=20, font=entry_font)
  208. save_file_entry_hide.grid(row=6, column=0, padx=10, pady=5)
  209. 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")))
  210. save_button_hide_file.grid(row=6, column=1, padx=10, pady=5)
  211.  
  212. # Add the refresh button to the left frame
  213. refresh_button = tk.Button(left_frame, text="Refresh", font=button_font, bg="#e06c75", fg="white", command=refresh_gui)
  214. refresh_button.grid(row=14, column=0, columnspan=2, padx=10, pady=5)
  215.  
  216. # Button for encoding
  217. hide_button = tk.Button(left_frame, text="AI Bind Image", font=button_font, bg="#98c379", fg="white", command=hide_images)
  218. hide_button.grid(row=7, column=0, columnspan=2, padx=10, pady=5)
  219.  
  220. # Unhiding Section
  221. tk.Label(left_frame, text="Convert the Genrate AI Image", font=('Arial', 14, 'bold'), bg="#282c34", fg="white").grid(row=8, column=0, columnspan=2, pady=20)
  222.  
  223. # Input for Encoded Image to unhide from
  224. tk.Label(left_frame, text="Select Generated Image", font=label_font, bg="#282c34", fg="white").grid(row=9, column=0, sticky="e", padx=10)
  225. original_entry_unhide = tk.Entry(left_frame, width=20, font=entry_font)
  226. original_entry_unhide.grid(row=10, column=0, padx=10, pady=5)
  227. 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()))
  228. browse_button_unhide_image.grid(row=10, column=1, padx=10, pady=5)
  229.  
  230. # Input for Save Location of extracted file
  231. tk.Label(left_frame, text="Save AI File Type (File.png):", font=label_font, bg="#282c34", fg="white").grid(row=11, column=0, sticky="e", padx=10)
  232. save_file_entry_unhide = tk.Entry(left_frame, width=20, font=entry_font)
  233. save_file_entry_unhide.grid(row=12, column=0, padx=10, pady=5)
  234. 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()))
  235. save_button_unhide_file.grid(row=12, column=1, padx=10, pady=5)
  236.  
  237. # Button for decoding
  238. unhide_button = tk.Button(left_frame, text="Genrate AI Image", font=button_font, bg="#98c379", fg="white", command=unhide_images)
  239. unhide_button.grid(row=13, column=0, columnspan=2, padx=10, pady=5)
  240.  
  241. # Create a canvas for image display with scrollbar
  242. canvas = tk.Canvas(root, bg="#000000")
  243. canvas.grid(row=0, column=2, rowspan=10, sticky="nsew")
  244.  
  245. # Add vertical and horizontal scrollbars
  246. vertical_scroll = tk.Scrollbar(root, orient="vertical", command=canvas.yview)
  247. vertical_scroll.grid(row=0, column=3, rowspan=10, sticky="ns")
  248. canvas.configure(yscrollcommand=vertical_scroll.set)
  249.  
  250. horizontal_scroll = tk.Scrollbar(root, orient="horizontal", command=canvas.xview)
  251. horizontal_scroll.grid(row=10, column=2, sticky="ew")
  252. canvas.configure(xscrollcommand=horizontal_scroll.set)
  253.  
  254. # Create an image label on the canvas
  255. image_label = tk.Label(canvas, bg="#000000")
  256. canvas.create_window((0, 0), window=image_label, anchor='nw')
  257.  
  258. # Zoom slider
  259. zoom_slider = tk.Scale(root, from_=10, to=200, bg="lightgreen", orient='horizontal', command=update_zoom)
  260. zoom_slider.set(50)  # Set default zoom level to 100%
  261. zoom_slider.grid(row=11, column=2, columnspan=2, sticky='ew', padx=10)
  262.  
  263. # Set initial scroll region
  264. canvas.config(scrollregion=canvas.bbox(tk.ALL))
  265.  
  266. # Run the application
  267. root.mainloop()
  268.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement