Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import numpy as np
- from imageio.v2 import imread
- import tkinter as tk
- from tkinter import filedialog, ttk
- from PIL import Image, ImageTk
- header_len = 4 * 8 # uint32 bit length
- def read_image(img_path):
- img = np.array(imread(img_path), dtype=np.uint8)
- orig_shape = img.shape
- return img.flatten(), orig_shape
- def read_file(file_path):
- file_bytes = open(file_path, "rb").read()
- return np.unpackbits(file_bytes)
- 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_file():
- filename = filedialog.askopenfilename(initialdir="/", title="Select Picture File")
- if filename:
- original_entry_unhide.delete(0, tk.END)
- original_entry_unhide.insert(0, filename)
- show_original_image(filename)
- def show_original_image(filename):
- 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
- 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 = original_entry_unhide.get()
- save_file = save_entry_unhide.get()
- if not os.path.isfile(original_file):
- print("Image file does not exist")
- return
- encoded_data, shape_orig = read_image(original_file)
- 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
- 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
- # 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
- root = tk.Tk()
- root.geometry("1000x660")
- root.title("Najeeb Generate AI Images")
- # Input for Picture Selection
- tk.Label(root, text="Select Picture File:").place(x=10, y=10)
- original_entry_unhide = tk.Entry(root)
- original_entry_unhide.place(x=120, y=10)
- browse_button = tk.Button(root, text="Browse", command=browse_file)
- browse_button.place(x=260, y=8)
- # Input for Save Location
- tk.Label(root, text="Select Save Location:").place(x=340, y=10)
- save_entry_unhide = tk.Entry(root)
- save_entry_unhide.place(x=470, y=10)
- browse_save_button = tk.Button(root, text="Browse", command=browse_save_location)
- browse_save_button.place(x=620, y=8)
- # Button to unhide images
- unhide_button = tk.Button(root, text="Generate AI Image", command=unhide_images, bg="#6699FF", fg="white")
- unhide_button.place(x=800, y=8)
- # Progress Bar
- unhide_progress_bar = ttk.Progressbar(root, orient='horizontal', mode='determinate')
- unhide_progress_bar.place(x=10, y=40, 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=70)
- 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=70)
- lbl2 = tk.Label(frame2, bg="#34495e")
- lbl2.place(x=10, y=100)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement