Advertisement
Najeebsk

AI-GENERATE-RAR.pyw

May 24th, 2024 (edited)
485
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.14 KB | None | 0 0
  1. import os
  2. import numpy as np
  3. import imageio.v2 as imageio
  4. import tkinter as tk
  5. from tkinter import filedialog, ttk
  6. from PIL import Image, ImageTk
  7. import rarfile
  8.  
  9. header_len = 4 * 8  # uint32 bit length
  10.  
  11. def read_image(img_path):
  12.     img = np.array(imageio.imread(img_path), dtype=np.uint8)
  13.     orig_shape = img.shape
  14.     return img.flatten(), orig_shape
  15.  
  16. def decode_data(encoded_data):
  17.     out_mask = np.ones_like(encoded_data)
  18.     output = np.bitwise_and(encoded_data, out_mask)
  19.     return output
  20.  
  21. def write_file(file_path, file_bit_array):
  22.     bytes_data = np.packbits(file_bit_array)
  23.     with open(file_path, 'wb') as f:
  24.         f.write(bytes_data)
  25.  
  26. def browse_rar_file():
  27.     filename = filedialog.askopenfilename(initialdir="/", title="Select RAR File", filetypes=[("RAR files", "*.rar")])
  28.     if filename:
  29.         rar_entry.delete(0, tk.END)
  30.         rar_entry.insert(0, filename)
  31.         load_rar_file_images(filename)
  32.  
  33. def load_rar_file_images(filename):
  34.     try:
  35.         with rarfile.RarFile(filename) as rf:
  36.             rf.setpassword(password_entry.get())  # Set the password for the RAR file
  37.             file_list = rf.namelist()
  38.             image_files = [file for file in file_list if file.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif'))]
  39.             image_dropdown['values'] = image_files
  40.             if image_files:
  41.                 image_dropdown.current(0)
  42.                 show_image_from_rar(filename, image_files[0])
  43.     except Exception as e:
  44.         print(f"Failed to read RAR file: {e}")
  45.  
  46. def show_image_from_rar(filename, image_name):
  47.     try:
  48.         with rarfile.RarFile(filename) as rf:
  49.             rf.setpassword(password_entry.get())  # Set the password for the RAR file
  50.             rf.extract(image_name, path="TEMP_DATA")
  51.             extracted_image_path = os.path.join("TEMP_DATA", image_name)
  52.             show_original_image(extracted_image_path)
  53.     except Exception as e:
  54.         print(f"Failed to extract image from RAR file: {e}")
  55.  
  56. def show_original_image(filename):
  57.     try:
  58.         original_image = Image.open(filename)
  59.         original_image.thumbnail((400, 400))  # Resize if needed
  60.         original_photo = ImageTk.PhotoImage(original_image)
  61.         lbl.config(image=original_photo)
  62.         lbl.image = original_photo
  63.     except Exception as e:
  64.         print(f"Failed to open image file: {e}")
  65.  
  66. def browse_save_location():
  67.     save_location = filedialog.asksaveasfilename(initialdir="/", title="Select Save Location", defaultextension=".jpg")
  68.     if save_location:
  69.         save_entry_unhide.delete(0, tk.END)
  70.         save_entry_unhide.insert(0, save_location)
  71.  
  72. def unhide_images():
  73.     original_file = rar_entry.get()
  74.     selected_image = image_dropdown.get()
  75.     save_file = save_entry_unhide.get()
  76.     password = password_entry.get()
  77.     if not os.path.isfile(original_file):
  78.         print("RAR file does not exist")
  79.         return
  80.  
  81.     try:
  82.         # Extract the selected image from the RAR file
  83.         with rarfile.RarFile(original_file) as rf:
  84.             rf.setpassword(password)  # Set the password for the RAR file
  85.             rf.extract(selected_image, path="TEMP_DATA")
  86.         extracted_image_path = os.path.join("TEMP_DATA", selected_image)
  87.     except rarfile.BadRarFile:
  88.         print("Failed to extract RAR file. Possibly wrong password or corrupted file.")
  89.         return
  90.  
  91.     # Proceed with the image decoding process
  92.     encoded_data, shape_orig = read_image(extracted_image_path)
  93.     data = decode_data(encoded_data)
  94.     el_array = np.packbits(data[:header_len])
  95.     extracted_len = el_array.view(np.uint32)[0]
  96.     data = data[header_len:extracted_len + header_len]
  97.     write_file(save_file, data)
  98.     print("Image decoded and saved")
  99.  
  100.     # Simulate a progress bar effect
  101.     progress = 0
  102.     while progress <= 100:
  103.         progress += 1
  104.         unhide_progress_bar['value'] = progress
  105.         root.update_idletasks()
  106.         root.after(50)  # Adjust speed of progress bar
  107.  
  108.     # Load and display the saved image
  109.     if os.path.isfile(save_file):
  110.         try:
  111.             saved_image = Image.open(save_file)
  112.             saved_image.thumbnail((400, 400))  # Resize if needed
  113.             saved_photo = ImageTk.PhotoImage(saved_image)
  114.             lbl2.config(image=saved_photo)
  115.             lbl2.image = saved_photo
  116.         except Exception as e:
  117.             print(f"Failed to open saved image: {e}")
  118.  
  119.     # Display the saved image with a slow vertical effect
  120.     for i in range(1, 101):
  121.         lbl2.place(y=i)  # Update y position gradually
  122.         root.update_idletasks()
  123.         root.after(10)  # Adjust speed of vertical effect
  124.  
  125. def clear_folder(folder_path):
  126.     try:
  127.         # List all files in the folder
  128.         files = os.listdir(folder_path)
  129.        
  130.         # Iterate through each file and delete it
  131.         for file in files:
  132.             file_path = os.path.join(folder_path, file)
  133.             if os.path.isfile(file_path):
  134.                 os.remove(file_path)
  135.        
  136.         print("Folder data cleared successfully.")
  137.     except Exception as e:
  138.         print(f"An error occurred: {e}")
  139.  
  140. # Example usage:
  141. folder_path = "TEMP_DATA"
  142. clear_folder(folder_path)
  143.  
  144. root = tk.Tk()
  145. root.geometry("1000x660")
  146. root.title("Najeeb Generate AI Images")
  147.  
  148. # Password Entry
  149. tk.Label(root, text="Password:").place(x=10, y=10)
  150. password_entry = tk.Entry(root, show="*")
  151. password_entry.place(x=80, y=10)
  152.  
  153. # Input for RAR File Selection
  154. tk.Label(root, text="Select RAR File:").place(x=260, y=10)
  155. rar_entry = tk.Entry(root)
  156. rar_entry.place(x=360, y=10)
  157. browse_rar_button = tk.Button(root, text="Browse", command=browse_rar_file)
  158. browse_rar_button.place(x=520, y=8)
  159.  
  160. # Dropdown for RAR file images
  161. tk.Label(root, text="Select Image from RAR:").place(x=600, y=10)
  162. image_dropdown = ttk.Combobox(root, state="readonly")
  163. image_dropdown.place(x=730, y=10)
  164. image_dropdown.bind("<<ComboboxSelected>>", lambda e: show_image_from_rar(rar_entry.get(), image_dropdown.get()))
  165.  
  166. # Input for Save Location
  167. tk.Label(root, text="Select Save Location:").place(x=10, y=50)
  168. save_entry_unhide = tk.Entry(root)
  169. save_entry_unhide.place(x=150, y=50)
  170. browse_save_button = tk.Button(root, text="Browse", command=browse_save_location)
  171. browse_save_button.place(x=310, y=48)
  172.  
  173. # Button to unhide images
  174. unhide_button = tk.Button(root, text="Generate AI Image", command=unhide_images, bg="#6699FF", fg="white")
  175. unhide_button.place(x=400, y=48)
  176.  
  177. # Button to Temp-Data Clear
  178. clear_button = tk.Button(root, text="Clear Data", command=lambda: clear_folder("TEMP_DATA"), bg="#6699FF", fg="white")
  179. clear_button.place(x=525, y=48)
  180.  
  181. # Progress Bar
  182. unhide_progress_bar = ttk.Progressbar(root, orient='horizontal', mode='determinate')
  183. unhide_progress_bar.place(x=10, y=80, width=980)
  184.  
  185. # Frame for Original Image
  186. f = tk.Frame(root, bd=3, bg="#2c3e50", width=500, height=640, relief=tk.GROOVE)
  187. f.place(x=5, y=110)
  188.  
  189. lbl = tk.Label(f, bg="#2c3e50")
  190. lbl.place(x=10, y=140)
  191.  
  192. # Frame for Generated AI Image
  193. frame2 = tk.Frame(root, bd=3, width=500, height=640, relief=tk.GROOVE, bg="#34495e")
  194. frame2.place(x=505, y=110)
  195.  
  196. lbl2 = tk.Label(frame2, bg="#34495e")
  197. lbl2.place(x=10, y=100)
  198.  
  199.  
  200. root.mainloop()
  201.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement