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, imwrite
- import tkinter as tk
- from tkinter import filedialog
- from PIL import Image, ImageTk
- max_value = 255 # max uint value per pixel per channel
- 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 write_image(img_path, img_data, shape):
- img_data = np.reshape(img_data, shape)
- imwrite(img_path, img_data)
- def bytes2array(byte_data):
- byte_array = np.frombuffer(byte_data, dtype=np.uint8)
- return np.unpackbits(byte_array)
- def array2bytes(bit_array):
- byte_array = np.packbits(bit_array)
- return byte_array.tobytes()
- def read_file(file_path):
- with open(file_path, "rb") as f:
- file_bytes = f.read()
- return bytes2array(file_bytes)
- def write_file(file_path, file_bit_array):
- bytes_data = array2bytes(file_bit_array)
- with open(file_path, 'wb') as f:
- f.write(bytes_data)
- def encode_data(image, file_data):
- or_mask = file_data
- and_mask = np.zeros_like(or_mask)
- and_mask = (and_mask + max_value - 1) + or_mask
- res = np.bitwise_or(image, or_mask)
- res = np.bitwise_and(res, and_mask)
- return res
- def decode_data(encoded_data):
- out_mask = np.ones_like(encoded_data)
- output = np.bitwise_and(encoded_data, out_mask)
- return output
- def update_zoom(event=None):
- if 'original_image' not in globals() or 'unhidden_image' not in globals():
- print("No image loaded to zoom")
- return
- zoom_value = zoom_slider.get() / 100 # Scale the value down to a fraction
- # Resizing both images
- resized_original = original_image.resize((int(original_image.width * zoom_value), int(original_image.height * zoom_value)))
- zoomed_original_photo = ImageTk.PhotoImage(resized_original)
- resized_unhidden = unhidden_image.resize((int(unhidden_image.width * zoom_value), int(unhidden_image.height * zoom_value)))
- zoomed_unhidden_photo = ImageTk.PhotoImage(resized_unhidden)
- # Update original image label
- original_image_label.config(image=zoomed_original_photo)
- original_image_label.image = zoomed_original_photo
- # Update unhidden image label
- unhidden_image_label.config(image=zoomed_unhidden_photo)
- unhidden_image_label.image = zoomed_unhidden_photo
- canvas.config(scrollregion=canvas.bbox(tk.ALL))
- def unhide_images():
- global original_image, unhidden_image # Ensure these are global variables
- original_file = original_entry_unhide.get()
- if not os.path.isfile(original_file):
- print("Image file does not exist")
- return
- # Read and decode the encoded image data
- encoded_data, shape_orig = read_image(original_file)
- data = decode_data(encoded_data)
- # Extract the length of the hidden file from the header
- el_array = np.packbits(data[:header_len])
- extracted_len = el_array.view(np.uint32)[0]
- # Extract the hidden file data based on the header length
- data = data[header_len:extracted_len + header_len]
- # Automatically determine the save path for the extracted file
- save_file = original_file.replace('.jpg', '_Generate_AI.png').replace('.png', '_Generate_AI.png')
- # Write the hidden file to the specified save location (as .png)
- write_file(save_file, data)
- print(f"File extracted and saved as {save_file}")
- # Load and display both the original and unhidden images
- original_image = Image.open(original_file) # Load the original image
- unhidden_image = Image.open(save_file) # Load the unhidden image
- update_image_display()
- def update_image_display():
- global original_image, unhidden_image
- resized_original = original_image.resize((200, 400)) # First-time view as 200x400
- original_photo = ImageTk.PhotoImage(resized_original)
- resized_unhidden = unhidden_image.resize((200, 400)) # First-time view as 200x400
- unhidden_photo = ImageTk.PhotoImage(resized_unhidden)
- # Update labels and configure scroll region
- original_image_label.config(image=original_photo)
- original_image_label.image = original_photo
- unhidden_image_label.config(image=unhidden_photo)
- unhidden_image_label.image = unhidden_photo
- canvas.config(scrollregion=canvas.bbox(tk.ALL))
- zoom_slider.set(100) # Reset the zoom slider to default
- def screenshot_both_images():
- if 'original_image' not in globals() or 'unhidden_image' not in globals():
- print("No images to screenshot")
- return
- # Create a new blank image to combine both images
- combined_width = original_image.width + unhidden_image.width
- combined_height = max(original_image.height, unhidden_image.height)
- combined_image = Image.new('RGB', (combined_width, combined_height))
- # Paste original image and unhidden image into the combined image
- combined_image.paste(original_image, (0, 0))
- combined_image.paste(unhidden_image, (original_image.width, 0))
- # Save the combined image
- save_path = filedialog.asksaveasfilename(defaultextension=".png", filetypes=[("PNG files", "*.png")])
- if save_path:
- combined_image.save(save_path)
- print(f"Screenshot saved at {save_path}")
- # Create the root window
- root = tk.Tk()
- root.geometry("1000x700")
- root.title("Najeeb AI Generator Image Tool")
- root.configure(bg="#282c34")
- # Grid configuration
- root.grid_rowconfigure(2, weight=1)
- root.grid_columnconfigure(0, weight=1)
- root.grid_columnconfigure(1, weight=1)
- root.grid_columnconfigure(2, weight=4) # Make column 2 wider for the canvas and zoom slider
- # Create a frame for the left side (fields and buttons)
- left_frame = tk.Frame(root, bg="#282c34")
- left_frame.grid(row=0, column=0, padx=5, pady=5, sticky='nsew')
- # Styling for labels and buttons
- label_font = ('Arial', 12, 'bold')
- entry_font = ('Arial', 11)
- button_font = ('Arial', 12, 'bold')
- button_color = "#61afef"
- button_fg = "white"
- # Input for Encoded Image to unhide from
- tk.Label(left_frame, text="Select AI Image", font=label_font, bg="#282c34", fg="white").grid(row=0, column=0, sticky="e", padx=5)
- original_entry_unhide = tk.Entry(left_frame, width=20, font=entry_font)
- original_entry_unhide.grid(row=0, column=1, padx=5, pady=5)
- browse_button_unhide_image = tk.Button(left_frame, text="Browse", font=button_font, bg=button_color, fg=button_fg, command=lambda: original_entry_unhide.insert(0, filedialog.askopenfilename()))
- browse_button_unhide_image.grid(row=0, column=2, padx=5, pady=5)
- # Button for decoding
- unhide_button = tk.Button(left_frame, text="Generate AI Image", font=button_font, bg="#98c379", fg="white", command=unhide_images)
- unhide_button.grid(row=0, column=3, columnspan=3, padx=10, pady=5)
- # Button for taking a screenshot of both images
- screenshot_button = tk.Button(left_frame, text="Screenshot Both Images", font=button_font, bg=button_color, fg=button_fg, command=screenshot_both_images)
- screenshot_button.grid(row=0, column=6, columnspan=3, padx=10, pady=5)
- # Create a frame for the canvas and scrollbar
- canvas_frame = tk.Frame(root)
- canvas_frame.grid(row=2, column=0, columnspan=3, sticky='nsew')
- # Create a canvas
- canvas = tk.Canvas(canvas_frame, bg="black")
- canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
- # Add a scrollbar
- scrollbar = tk.Scrollbar(canvas_frame, command=canvas.yview)
- scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
- canvas.config(yscrollcommand=scrollbar.set)
- # Create a frame inside the canvas
- image_frame = tk.Frame(canvas, bg="black")
- canvas.create_window((0, 0), window=image_frame, anchor='nw')
- # Labels for the original and unhidden images
- original_image_label = tk.Label(image_frame, bg="black")
- original_image_label.grid(row=0, column=0, padx=5, pady=5)
- unhidden_image_label = tk.Label(image_frame, bg="black")
- unhidden_image_label.grid(row=0, column=1, padx=5, pady=5)
- # Zoom slider
- zoom_slider = tk.Scale(left_frame, from_=10, to=300, orient="horizontal", label="Zoom (%)", command=update_zoom)
- zoom_slider.set(100) # Set default zoom value
- zoom_slider.grid(row=1, column=0, columnspan=3, padx=5, pady=5)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement