Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import numpy as np
- import imageio.v2 as imageio
- import tkinter as tk
- from tkinter import filedialog, ttk
- from PIL import Image, ImageTk
- import rarfile
- header_len = 4 * 8 # uint32 bit length
- def read_image(img_path):
- img = np.array(imageio.imread(img_path), dtype=np.uint8)
- orig_shape = img.shape
- return img.flatten(), orig_shape
- def decode_data(encoded_data):
- out_mask = np.ones_like(encoded_data)
- output = np.bitwise_and(encoded_data, out_mask)
- return output
- def write_file(file_path, file_bit_array):
- bytes_data = np.packbits(file_bit_array)
- with open(file_path, 'wb') as f:
- f.write(bytes_data)
- def browse_rar_file():
- filename = filedialog.askopenfilename(initialdir="/", title="Select RAR File", filetypes=[("RAR files", "*.rar")])
- if filename:
- rar_entry.delete(0, tk.END)
- rar_entry.insert(0, filename)
- load_rar_file_images(filename)
- def load_rar_file_images(filename):
- try:
- with rarfile.RarFile(filename) as rf:
- rf.setpassword(password_entry.get()) # Set the password for the RAR file
- file_list = rf.namelist()
- image_files = sorted([file for file in file_list if file.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif'))])
- image_dropdown['values'] = image_files
- if image_files:
- image_dropdown.current(0)
- show_image_from_rar(filename, image_files[0])
- except Exception as e:
- print(f"Failed to read RAR file: {e}")
- def show_image_from_rar(filename, image_name):
- try:
- with rarfile.RarFile(filename) as rf:
- rf.setpassword(password_entry.get()) # Set the password for the RAR file
- rf.extract(image_name, path="TEMP_DATA")
- extracted_image_path = os.path.join("TEMP_DATA", image_name)
- show_original_image(extracted_image_path)
- except Exception as e:
- print(f"Failed to extract image from RAR file: {e}")
- def show_original_image(filename):
- global original_image
- try:
- original_image = Image.open(filename)
- original_image.thumbnail((400, 400)) # Resize if needed
- update_zoom_image() # Show the image at the initial zoom level
- except Exception as e:
- print(f"Failed to open image file: {e}")
- def browse_save_location():
- save_location = filedialog.asksaveasfilename(initialdir="/", title="Select Save Location", defaultextension=".jpg")
- if save_location:
- save_entry_unhide.delete(0, tk.END)
- save_entry_unhide.insert(0, save_location)
- def unhide_images():
- original_file = rar_entry.get()
- selected_image = image_dropdown.get()
- save_file = save_entry_unhide.get()
- password = password_entry.get()
- if not os.path.isfile(original_file):
- print("RAR file does not exist")
- return
- try:
- # Extract the selected image from the RAR file
- with rarfile.RarFile(original_file) as rf:
- rf.setpassword(password) # Set the password for the RAR file
- rf.extract(selected_image, path="TEMP_DATA")
- extracted_image_path = os.path.join("TEMP_DATA", selected_image)
- except rarfile.BadRarFile:
- print("Failed to extract RAR file. Possibly wrong password or corrupted file.")
- return
- # Proceed with the image decoding process
- encoded_data, shape_orig = read_image(extracted_image_path)
- data = decode_data(encoded_data)
- el_array = np.packbits(data[:header_len])
- extracted_len = el_array.view(np.uint32)[0]
- data = data[header_len:extracted_len + header_len]
- write_file(save_file, data)
- print("Image decoded and saved")
- # Simulate a progress bar effect
- progress = 0
- while progress <= 100:
- progress += 1
- unhide_progress_bar['value'] = progress
- root.update_idletasks()
- root.after(50) # Adjust speed of progress bar
- # Load and display the saved image
- if os.path.isfile(save_file):
- try:
- global saved_image
- saved_image = Image.open(save_file)
- saved_image.thumbnail((400, 400)) # Resize if needed
- update_zoom_image_saved() # Show the saved image at the initial zoom level
- except Exception as e:
- print(f"Failed to open saved image: {e}")
- def clear_folder(folder_path):
- try:
- # List all files in the folder
- files = os.listdir(folder_path)
- # Iterate through each file and delete it
- for file in files:
- file_path = os.path.join(folder_path, file)
- if os.path.isfile(file_path):
- os.remove(file_path)
- print("Folder data cleared successfully.")
- except Exception as e:
- print(f"An error occurred: {e}")
- def update_zoom_image():
- try:
- zoom_factor = zoom_slider.get()
- width, height = original_image.size
- new_size = (int(width * zoom_factor), int(height * zoom_factor))
- zoomed_image = original_image.resize(new_size, Image.LANCZOS)
- zoomed_photo = ImageTk.PhotoImage(zoomed_image)
- lbl.config(image=zoomed_photo)
- lbl.image = zoomed_photo
- # Update the scroll region of the canvas to the new size of the image
- canvas.config(scrollregion=canvas.bbox(tk.ALL))
- except Exception as e:
- print(f"Failed to update zoom image: {e}")
- def update_zoom_image_saved():
- try:
- zoom_factor = zoom_slider_saved.get()
- width, height = saved_image.size
- new_size = (int(width * zoom_factor), int(height * zoom_factor))
- zoomed_image_saved = saved_image.resize(new_size, Image.LANCZOS)
- zoomed_photo_saved = ImageTk.PhotoImage(zoomed_image_saved)
- lbl2.config(image=zoomed_photo_saved)
- lbl2.image = zoomed_photo_saved
- # Update the scroll region of the canvas to the new size of the image
- canvas2.config(scrollregion=canvas2.bbox(tk.ALL))
- except Exception as e:
- print(f"Failed to update zoom image: {e}")
- # Clear folder on startup
- folder_path = "TEMP_DATA"
- clear_folder(folder_path)
- root = tk.Tk()
- root.geometry("1000x660")
- root.title("Najeeb Generate AI Images")
- # Password Entry
- tk.Label(root, text="Password:").place(x=10, y=10)
- password_entry = tk.Entry(root, show="*")
- password_entry.place(x=80, y=10)
- # Input for RAR File Selection
- tk.Label(root, text="Select RAR File:").place(x=260, y=10)
- rar_entry = tk.Entry(root)
- rar_entry.place(x=360, y=10)
- browse_rar_button = tk.Button(root, text="Browse", command=browse_rar_file)
- browse_rar_button.place(x=520, y=8)
- # Dropdown for RAR file images
- tk.Label(root, text="Select Image from RAR:").place(x=600, y=10)
- image_dropdown = ttk.Combobox(root, state="readonly")
- image_dropdown.place(x=730, y=10)
- image_dropdown.bind("<<ComboboxSelected>>", lambda e: show_image_from_rar(rar_entry.get(), image_dropdown.get()))
- # Input for Save Location
- tk.Label(root, text="Select Save Location:").place(x=10, y=50)
- save_entry_unhide = tk.Entry(root)
- save_entry_unhide.place(x=130, y=50)
- browse_save_button = tk.Button(root, text="Browse", command=browse_save_location)
- browse_save_button.place(x=280, y=48)
- # Button to unhide images
- unhide_button = tk.Button(root, text="Generate AI Image", command=unhide_images, bg="#6699FF", fg="white")
- unhide_button.place(x=350, y=48)
- # Button to Temp-Data Clear
- clear_button = tk.Button(root, text="Clear Data", command=lambda: clear_folder("TEMP_DATA"), bg="#6699FF", fg="white")
- clear_button.place(x=480, y=48)
- # Zoom Slider
- tk.Label(root, text="Zoom Image:").place(x=560, y=50)
- zoom_slider = tk.Scale(root, from_=0.1, to=10, orient=tk.HORIZONTAL, label="", resolution=0.1, command=lambda e: update_zoom_image())
- zoom_slider.set(1)
- zoom_slider.place(x=650, y=38)
- # Progress Bar
- unhide_progress_bar = ttk.Progressbar(root, orient='horizontal', mode='determinate')
- unhide_progress_bar.place(x=10, y=80, width=980)
- # Frame for Original Image with Scrollbars
- frame_original = tk.Frame(root, bd=3, bg="#2c3e50", width=480, height=540, relief=tk.GROOVE)
- frame_original.place(x=5, y=110)
- canvas = tk.Canvas(frame_original, bg="#2c3e50", width=470, height=520)
- scroll_y = tk.Scrollbar(frame_original, orient="vertical", command=canvas.yview)
- scroll_x = tk.Scrollbar(frame_original, orient="horizontal", command=canvas.xview)
- canvas.configure(yscrollcommand=scroll_y.set, xscrollcommand=scroll_x.set)
- scroll_y.pack(side="right", fill="y")
- scroll_x.pack(side="bottom", fill="x")
- canvas.pack(side="left", fill="both", expand=True)
- lbl = tk.Label(canvas, bg="#2c3e50")
- canvas.create_window((0, 0), window=lbl, anchor="nw")
- # Frame for Generated AI Image with Scrollbars
- frame_generated = tk.Frame(root, bd=3, width=480, height=540, relief=tk.GROOVE, bg="#34495e")
- frame_generated.place(x=505, y=110)
- canvas2 = tk.Canvas(frame_generated, bg="#34495e", width=470, height=520)
- scroll_y2 = tk.Scrollbar(frame_generated, orient="vertical", command=canvas2.yview)
- scroll_x2 = tk.Scrollbar(frame_generated, orient="horizontal", command=canvas2.xview)
- canvas2.configure(yscrollcommand=scroll_y2.set, xscrollcommand=scroll_x2.set)
- scroll_y2.pack(side="right", fill="y")
- scroll_x2.pack(side="bottom", fill="x")
- canvas2.pack(side="left", fill="both", expand=True)
- lbl2 = tk.Label(canvas2, bg="#34495e")
- canvas2.create_window((0, 0), window=lbl2, anchor="nw")
- # Zoom Slider for the saved image
- tk.Label(root, text="Zoom AI Image:").place(x=770, y=50)
- 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())
- zoom_slider_saved.set(1)
- zoom_slider_saved.place(x=870, y=38)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement