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 = [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):
- try:
- original_image = Image.open(filename)
- original_image.thumbnail((400, 400)) # Resize if needed
- original_photo = ImageTk.PhotoImage(original_image)
- lbl.config(image=original_photo)
- lbl.image = original_photo
- 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:
- saved_image = Image.open(save_file)
- saved_image.thumbnail((400, 400)) # Resize if needed
- saved_photo = ImageTk.PhotoImage(saved_image)
- lbl2.config(image=saved_photo)
- lbl2.image = saved_photo
- except Exception as e:
- print(f"Failed to open saved image: {e}")
- # Display the saved image with a slow vertical effect
- for i in range(1, 101):
- lbl2.place(y=i) # Update y position gradually
- root.update_idletasks()
- root.after(10) # Adjust speed of vertical effect
- 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}")
- # Example usage:
- 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=150, y=50)
- browse_save_button = tk.Button(root, text="Browse", command=browse_save_location)
- browse_save_button.place(x=310, 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=400, 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=525, y=48)
- # 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
- f = tk.Frame(root, bd=3, bg="#2c3e50", width=500, height=640, relief=tk.GROOVE)
- f.place(x=5, y=110)
- lbl = tk.Label(f, bg="#2c3e50")
- lbl.place(x=10, y=140)
- # Frame for Generated AI Image
- frame2 = tk.Frame(root, bd=3, width=500, height=640, relief=tk.GROOVE, bg="#34495e")
- frame2.place(x=505, y=110)
- lbl2 = tk.Label(frame2, bg="#34495e")
- lbl2.place(x=10, y=100)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement