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):
- file_bytes = open(file_path, "rb").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()
- save_file = save_file_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]
- # Write the hidden file to the specified save location (as .png)
- save_file = save_file.replace('.jpg', '.png') # Force saving 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
- 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(30) # Reset the zoom slider to default
- def save_images():
- original_save_path = filedialog.asksaveasfilename(defaultextension=".png", filetypes=[("PNG files", "*.png")])
- unhidden_save_path = filedialog.asksaveasfilename(defaultextension=".png", filetypes=[("PNG files", "*.png")])
- if original_save_path:
- original_image.save(original_save_path)
- print(f"Original image saved at {original_save_path}")
- if unhidden_save_path:
- unhidden_image.save(unhidden_save_path)
- print(f"Unhidden image saved at {unhidden_save_path}")
- 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 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=12, 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)
- # Input for Save Location of extracted file
- tk.Label(left_frame, text="Save AI:", font=label_font, bg="#282c34", fg="white").grid(row=0, column=3, sticky="e", padx=10)
- save_file_entry_unhide = tk.Entry(left_frame, width=12, font=entry_font)
- save_file_entry_unhide.grid(row=0, column=4, padx=5, pady=5)
- save_button_unhide_file = tk.Button(left_frame, text="Save As", font=button_font, bg=button_color, fg=button_fg, command=lambda: save_file_entry_unhide.insert(0, filedialog.asksaveasfilename()))
- save_button_unhide_file.grid(row=0, column=5, padx=10, pady=5)
- # Button for decoding
- unhide_button = tk.Button(left_frame, text="Generate AI", font=button_font, bg="#98c379", fg="white", command=unhide_images)
- unhide_button.grid(row=0, column=6, 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=7, padx=10, pady=5)
- # Create a canvas for image display with scrollbar
- canvas = tk.Canvas(root, bg="#000000")
- canvas.grid(row=2, column=0, columnspan=3, sticky="nsew")
- # Add vertical and horizontal scrollbars
- vertical_scroll = tk.Scrollbar(root, orient="vertical", command=canvas.yview)
- vertical_scroll.grid(row=2, column=3, sticky="ns")
- canvas.configure(yscrollcommand=vertical_scroll.set)
- horizontal_scroll = tk.Scrollbar(root, orient="horizontal", command=canvas.xview)
- horizontal_scroll.grid(row=3, column=0, columnspan=3, sticky="ew")
- canvas.configure(xscrollcommand=horizontal_scroll.set)
- # Frame to hold images inside canvas
- image_frame = tk.Frame(canvas, bg="#000000")
- canvas.create_window((0, 0), window=image_frame, anchor="nw")
- # Labels to hold original and unhidden images
- original_image_label = tk.Label(image_frame)
- original_image_label.grid(row=0, column=0, padx=10, pady=10)
- unhidden_image_label = tk.Label(image_frame)
- unhidden_image_label.grid(row=0, column=1, padx=10, pady=10)
- # Zoom slider
- zoom_slider = tk.Scale(root, from_=10, to=200, orient="horizontal", command=update_zoom, font=label_font, bg="#282c34", fg="white")
- zoom_slider.grid(row=1, column=0, columnspan=3, sticky="ew", padx=10)
- # Save button for both images
- #save_images_button = tk.Button(left_frame, text="Save Both Images", font=button_font, bg="#e06c75", fg="white", command=save_images)
- #save_images_button.grid(row=1, column=6, padx=10, pady=10)
- # Start the main event loop
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement