Advertisement
Najeebsk

AI-GENERATE2.0.pyw

May 24th, 2024
539
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.16 KB | None | 0 0
  1. import os
  2. import numpy as np
  3. from imageio.v2 import imread
  4. import tkinter as tk
  5. from tkinter import filedialog, ttk
  6. from PIL import Image, ImageTk
  7.  
  8. header_len = 4 * 8  # uint32 bit length
  9.  
  10. def read_image(img_path):
  11.     img = np.array(imread(img_path), dtype=np.uint8)
  12.     orig_shape = img.shape
  13.     return img.flatten(), orig_shape
  14.  
  15. def read_file(file_path):
  16.     file_bytes = open(file_path, "rb").read()
  17.     return np.unpackbits(file_bytes)
  18.  
  19. def decode_data(encoded_data):
  20.     out_mask = np.ones_like(encoded_data)
  21.     output = np.bitwise_and(encoded_data, out_mask)
  22.     return output
  23.  
  24. def write_file(file_path, file_bit_array):
  25.     bytes_data = np.packbits(file_bit_array)
  26.     with open(file_path, 'wb') as f:
  27.         f.write(bytes_data)
  28.  
  29. def browse_file():
  30.     filename = filedialog.askopenfilename(initialdir="/", title="Select Picture File")
  31.     if filename:
  32.         original_entry_unhide.delete(0, tk.END)
  33.         original_entry_unhide.insert(0, filename)
  34.         show_original_image(filename)
  35.  
  36. def show_original_image(filename):
  37.     original_image = Image.open(filename)
  38.     original_image.thumbnail((400, 400))  # Resize if needed
  39.     original_photo = ImageTk.PhotoImage(original_image)
  40.     lbl.config(image=original_photo)
  41.     lbl.image = original_photo
  42.  
  43. def browse_save_location():
  44.     save_location = filedialog.asksaveasfilename(initialdir="/", title="Select Save Location", defaultextension=".jpg")
  45.     if save_location:
  46.         save_entry_unhide.delete(0, tk.END)
  47.         save_entry_unhide.insert(0, save_location)
  48.  
  49. def unhide_images():
  50.     original_file = original_entry_unhide.get()
  51.     save_file = save_entry_unhide.get()
  52.     if not os.path.isfile(original_file):
  53.         print("Image file does not exist")
  54.         return
  55.  
  56.     encoded_data, shape_orig = read_image(original_file)
  57.     data = decode_data(encoded_data)
  58.     el_array = np.packbits(data[:header_len])
  59.     extracted_len = el_array.view(np.uint32)[0]
  60.     data = data[header_len:extracted_len + header_len]
  61.     write_file(save_file, data)
  62.     print("Image decoded and saved")
  63.  
  64.     # Simulate a progress bar effect
  65.     progress = 0
  66.     while progress <= 100:
  67.         progress += 1
  68.         unhide_progress_bar['value'] = progress
  69.         root.update_idletasks()
  70.         root.after(50)  # Adjust speed of progress bar
  71.  
  72.     # Load and display the saved image
  73.     saved_image = Image.open(save_file)
  74.     saved_image.thumbnail((400, 400))  # Resize if needed
  75.     saved_photo = ImageTk.PhotoImage(saved_image)
  76.     lbl2.config(image=saved_photo)
  77.     lbl2.image = saved_photo
  78.  
  79.     # Display the saved image with a slow vertical effect
  80.     for i in range(1, 101):
  81.         lbl2.place(y=i)  # Update y position gradually
  82.         root.update_idletasks()
  83.         root.after(10)  # Adjust speed of vertical effect
  84.  
  85. root = tk.Tk()
  86. root.geometry("1000x660")
  87. root.title("Najeeb Generate AI Images")
  88.  
  89. # Input for Picture Selection
  90. tk.Label(root, text="Select Picture File:").place(x=10, y=10)
  91. original_entry_unhide = tk.Entry(root)
  92. original_entry_unhide.place(x=120, y=10)
  93. browse_button = tk.Button(root, text="Browse", command=browse_file)
  94. browse_button.place(x=260, y=8)
  95.  
  96. # Input for Save Location
  97. tk.Label(root, text="Select Save Location:").place(x=340, y=10)
  98. save_entry_unhide = tk.Entry(root)
  99. save_entry_unhide.place(x=470, y=10)
  100. browse_save_button = tk.Button(root, text="Browse", command=browse_save_location)
  101. browse_save_button.place(x=620, y=8)
  102.  
  103. # Button to unhide images
  104. unhide_button = tk.Button(root, text="Generate AI Image", command=unhide_images, bg="#6699FF", fg="white")
  105. unhide_button.place(x=800, y=8)
  106.  
  107. # Progress Bar
  108. unhide_progress_bar = ttk.Progressbar(root, orient='horizontal', mode='determinate')
  109. unhide_progress_bar.place(x=10, y=40, width=980)
  110.  
  111. # Frame for Original Image
  112. f = tk.Frame(root, bd=3, bg="#2c3e50", width=500, height=640, relief=tk.GROOVE)
  113. f.place(x=5, y=70)
  114.  
  115. lbl = tk.Label(f, bg="#2c3e50")
  116. lbl.place(x=10, y=140)
  117.  
  118. # Frame for Generated AI Image
  119. frame2 = tk.Frame(root, bd=3, width=500, height=640, relief=tk.GROOVE, bg="#34495e")
  120. frame2.place(x=505, y=70)
  121.  
  122. lbl2 = tk.Label(frame2, bg="#34495e")
  123. lbl2.place(x=10, y=100)
  124.  
  125. root.mainloop()
  126.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement