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
- 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")
- original_entry_unhide.delete(0, tk.END)
- original_entry_unhide.insert(0, filename)
- def browse_save_location():
- save_location = filedialog.asksaveasfilename(initialdir="/", title="Select Save Location", defaultextension=".jpg")
- 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")
- # Load and display the original image
- original_image = Image.open(original_file)
- original_image.thumbnail((shape_orig[0] // 3, shape_orig[1])) # Resize if needed
- original_photo = ImageTk.PhotoImage(original_image)
- lbl.config(image=original_photo)
- lbl.image = original_photo
- # Load and display the saved image
- saved_image = Image.open(save_file)
- saved_image.thumbnail((shape_orig[0] // 3, shape_orig[1])) # Resize if needed
- saved_photo = ImageTk.PhotoImage(saved_image)
- lbl2.config(image=saved_photo)
- lbl2.image = saved_photo
- 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=140, y=10)
- browse_button = tk.Button(root, text="Browse", command=browse_file)
- browse_button.place(x=330, y=8)
- # Input for Save Location
- tk.Label(root, text="Select Save Location:").place(x=10, y=40)
- save_entry_unhide = tk.Entry(root)
- save_entry_unhide.place(x=140, y=40)
- browse_save_button = tk.Button(root, text="Browse", command=browse_save_location)
- browse_save_button.place(x=330, y=38)
- # Button to unhide images
- unhide_button = tk.Button(root, text="Generate AI Image", command=unhide_images, bg="#6699FF", fg="white")
- unhide_button.place(x=600, y=8)
- # 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