SHARE
    TWEET
    Najeebsk

    AI-GENERATE.pyw

    May 30th, 2024 (edited)
    1,117
    0
    Never
    Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
    Python 9.72 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. header_len = 4 * 8 # uint32 bit length
    9. def read_image(img_path):
    10. img = np.array(imageio.imread(img_path), dtype=np.uint8)
    11. orig_shape = img.shape
    12. return img.flatten(), orig_shape
    13. def decode_data(encoded_data):
    14. out_mask = np.ones_like(encoded_data)
    15. output = np.bitwise_and(encoded_data, out_mask)
    16. return output
    17. def write_file(file_path, file_bit_array):
    18. bytes_data = np.packbits(file_bit_array)
    19. with open(file_path, 'wb') as f:
    20. f.write(bytes_data)
    21. def browse_rar_file():
    22. filename = filedialog.askopenfilename(initialdir="/", title="Select RAR File", filetypes=[("RAR files", "*.rar")])
    23. if filename:
    24. rar_entry.delete(0, tk.END)
    25. rar_entry.insert(0, filename)
    26. load_rar_file_images(filename)
    27. def load_rar_file_images(filename):
    28. try:
    29. with rarfile.RarFile(filename) as rf:
    30. rf.setpassword(password_entry.get()) # Set the password for the RAR file
    31. file_list = rf.namelist()
    32. image_files = sorted([file for file in file_list if file.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif'))])
    33. image_dropdown['values'] = image_files
    34. if image_files:
    35. image_dropdown.current(0)
    36. show_image_from_rar(filename, image_files[0])
    37. except Exception as e:
    38. print(f"Failed to read RAR file: {e}")
    39. def show_image_from_rar(filename, image_name):
    40. try:
    41. with rarfile.RarFile(filename) as rf:
    42. rf.setpassword(password_entry.get()) # Set the password for the RAR file
    43. rf.extract(image_name, path="TEMP_DATA")
    44. extracted_image_path = os.path.join("TEMP_DATA", image_name)
    45. show_original_image(extracted_image_path)
    46. except Exception as e:
    47. print(f"Failed to extract image from RAR file: {e}")
    48. def show_original_image(filename):
    49. global original_image
    50. try:
    51. original_image = Image.open(filename)
    52. original_image.thumbnail((400, 400)) # Resize if needed
    53. update_zoom_image() # Show the image at the initial zoom level
    54. except Exception as e:
    55. print(f"Failed to open image file: {e}")
    56. def browse_save_location():
    57. save_location = filedialog.asksaveasfilename(initialdir="/", title="Select Save Location", defaultextension=".jpg")
    58. if save_location:
    59. save_entry_unhide.delete(0, tk.END)
    60. save_entry_unhide.insert(0, save_location)
    61. def unhide_images():
    62. original_file = rar_entry.get()
    63. selected_image = image_dropdown.get()
    64. save_file = save_entry_unhide.get()
    65. password = password_entry.get()
    66. if not os.path.isfile(original_file):
    67. print("RAR file does not exist")
    68. return
    69. try:
    70. # Extract the selected image from the RAR file
    71. with rarfile.RarFile(original_file) as rf:
    72. rf.setpassword(password) # Set the password for the RAR file
    73. rf.extract(selected_image, path="TEMP_DATA")
    74. extracted_image_path = os.path.join("TEMP_DATA", selected_image)
    75. except rarfile.BadRarFile:
    76. print("Failed to extract RAR file. Possibly wrong password or corrupted file.")
    77. return
    78. # Proceed with the image decoding process
    79. encoded_data, shape_orig = read_image(extracted_image_path)
    80. data = decode_data(encoded_data)
    81. el_array = np.packbits(data[:header_len])
    82. extracted_len = el_array.view(np.uint32)[0]
    83. data = data[header_len:extracted_len + header_len]
    84. write_file(save_file, data)
    85. print("Image decoded and saved")
    86. # Simulate a progress bar effect
    87. progress = 0
    88. while progress <= 100:
    89. progress += 1
    90. unhide_progress_bar['value'] = progress
    91. root.update_idletasks()
    92. root.after(50) # Adjust speed of progress bar
    93. # Load and display the saved image
    94. if os.path.isfile(save_file):
    95. try:
    96. global saved_image
    97. saved_image = Image.open(save_file)
    98. saved_image.thumbnail((400, 400)) # Resize if needed
    99. update_zoom_image_saved() # Show the saved image at the initial zoom level
    100. except Exception as e:
    101. print(f"Failed to open saved image: {e}")
    102. def clear_folder(folder_path):
    103. try:
    104. # List all files in the folder
    105. files = os.listdir(folder_path)
    106. # Iterate through each file and delete it
    107. for file in files:
    108. file_path = os.path.join(folder_path, file)
    109. if os.path.isfile(file_path):
    110. os.remove(file_path)
    111. print("Folder data cleared successfully.")
    112. except Exception as e:
    113. print(f"An error occurred: {e}")
    114. def update_zoom_image():
    115. try:
    116. zoom_factor = zoom_slider.get()
    117. width, height = original_image.size
    118. new_size = (int(width * zoom_factor), int(height * zoom_factor))
    119. zoomed_image = original_image.resize(new_size, Image.LANCZOS)
    120. zoomed_photo = ImageTk.PhotoImage(zoomed_image)
    121. lbl.config(image=zoomed_photo)
    122. lbl.image = zoomed_photo
    123. # Update the scroll region of the canvas to the new size of the image
    124. canvas.config(scrollregion=canvas.bbox(tk.ALL))
    125. except Exception as e:
    126. print(f"Failed to update zoom image: {e}")
    127. def update_zoom_image_saved():
    128. try:
    129. zoom_factor = zoom_slider_saved.get()
    130. width, height = saved_image.size
    131. new_size = (int(width * zoom_factor), int(height * zoom_factor))
    132. zoomed_image_saved = saved_image.resize(new_size, Image.LANCZOS)
    133. zoomed_photo_saved = ImageTk.PhotoImage(zoomed_image_saved)
    134. lbl2.config(image=zoomed_photo_saved)
    135. lbl2.image = zoomed_photo_saved
    136. # Update the scroll region of the canvas to the new size of the image
    137. canvas2.config(scrollregion=canvas2.bbox(tk.ALL))
    138. except Exception as e:
    139. print(f"Failed to update zoom image: {e}")
    140. # Clear folder on startup
    141. folder_path = "TEMP_DATA"
    142. clear_folder(folder_path)
    143. root = tk.Tk()
    144. root.geometry("1000x660")
    145. root.title("Najeeb Generate AI Images")
    146. # Password Entry
    147. tk.Label(root, text="Password:").place(x=10, y=10)
    148. password_entry = tk.Entry(root, show="*")
    149. password_entry.place(x=80, y=10)
    150. # Input for RAR File Selection
    151. tk.Label(root, text="Select RAR File:").place(x=260, y=10)
    152. rar_entry = tk.Entry(root)
    153. rar_entry.place(x=360, y=10)
    154. browse_rar_button = tk.Button(root, text="Browse", command=browse_rar_file)
    155. browse_rar_button.place(x=520, y=8)
    156. # Dropdown for RAR file images
    157. tk.Label(root, text="Select Image from RAR:").place(x=600, y=10)
    158. image_dropdown = ttk.Combobox(root, state="readonly")
    159. image_dropdown.place(x=730, y=10)
    160. image_dropdown.bind("<<ComboboxSelected>>", lambda e: show_image_from_rar(rar_entry.get(), image_dropdown.get()))
    161. # Input for Save Location
    162. tk.Label(root, text="Select Save Location:").place(x=10, y=50)
    163. save_entry_unhide = tk.Entry(root)
    164. save_entry_unhide.place(x=130, y=50)
    165. browse_save_button = tk.Button(root, text="Browse", command=browse_save_location)
    166. browse_save_button.place(x=280, y=48)
    167. # Button to unhide images
    168. unhide_button = tk.Button(root, text="Generate AI Image", command=unhide_images, bg="#6699FF", fg="white")
    169. unhide_button.place(x=350, y=48)
    170. # Button to Temp-Data Clear
    171. clear_button = tk.Button(root, text="Clear Data", command=lambda: clear_folder("TEMP_DATA"), bg="#6699FF", fg="white")
    172. clear_button.place(x=480, y=48)
    173. # Zoom Slider
    174. tk.Label(root, text="Zoom Image:").place(x=560, y=50)
    175. zoom_slider = tk.Scale(root, from_=0.1, to=10, orient=tk.HORIZONTAL, label="", resolution=0.1, command=lambda e: update_zoom_image())
    176. zoom_slider.set(1)
    177. zoom_slider.place(x=650, y=38)
    178. # Progress Bar
    179. unhide_progress_bar = ttk.Progressbar(root, orient='horizontal', mode='determinate')
    180. unhide_progress_bar.place(x=10, y=80, width=980)
    181. # Frame for Original Image with Scrollbars
    182. frame_original = tk.Frame(root, bd=3, bg="#2c3e50", width=480, height=540, relief=tk.GROOVE)
    183. frame_original.place(x=5, y=110)
    184. canvas = tk.Canvas(frame_original, bg="#2c3e50", width=470, height=520)
    185. scroll_y = tk.Scrollbar(frame_original, orient="vertical", command=canvas.yview)
    186. scroll_x = tk.Scrollbar(frame_original, orient="horizontal", command=canvas.xview)
    187. canvas.configure(yscrollcommand=scroll_y.set, xscrollcommand=scroll_x.set)
    188. scroll_y.pack(side="right", fill="y")
    189. scroll_x.pack(side="bottom", fill="x")
    190. canvas.pack(side="left", fill="both", expand=True)
    191. lbl = tk.Label(canvas, bg="#2c3e50")
    192. canvas.create_window((0, 0), window=lbl, anchor="nw")
    193. # Frame for Generated AI Image with Scrollbars
    194. frame_generated = tk.Frame(root, bd=3, width=480, height=540, relief=tk.GROOVE, bg="#34495e")
    195. frame_generated.place(x=505, y=110)
    196. canvas2 = tk.Canvas(frame_generated, bg="#34495e", width=470, height=520)
    197. scroll_y2 = tk.Scrollbar(frame_generated, orient="vertical", command=canvas2.yview)
    198. scroll_x2 = tk.Scrollbar(frame_generated, orient="horizontal", command=canvas2.xview)
    199. canvas2.configure(yscrollcommand=scroll_y2.set, xscrollcommand=scroll_x2.set)
    200. scroll_y2.pack(side="right", fill="y")
    201. scroll_x2.pack(side="bottom", fill="x")
    202. canvas2.pack(side="left", fill="both", expand=True)
    203. lbl2 = tk.Label(canvas2, bg="#34495e")
    204. canvas2.create_window((0, 0), window=lbl2, anchor="nw")
    205. # Zoom Slider for the saved image
    206. tk.Label(root, text="Zoom AI Image:").place(x=770, y=50)
    207. zoom_slider_saved = tk.Scale(root, from_=0.1, to=10, orient=tk.HORIZONTAL, label="", resolution=0.1, command=lambda e: update_zoom_image_saved())
    208. zoom_slider_saved.set(1)
    209. zoom_slider_saved.place(x=870, y=38)
    210. root.mainloop()
    Advertisement
    Add Comment
    Please, Sign In to add comment
    Public Pastes
    We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
    Not a member of Pastebin yet?
    Sign Up, it unlocks many cool features!

    AltStyle によって変換されたページ (->オリジナル) /