Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- from tkinter import Label
- import numpy as np
- from imageio import imread, imwrite
- import tkinter as tk
- from PIL import Image, ImageTk
- import imageio.v2 as imageio # Use v2 to suppress deprecation warning
- max_value = 255 # max uint value per pixel per channel
- header_len = 4 * 8 # uint32 bit length
- def read_image(img_path):
- """
- Reads an image from file and flattens it.
- Args:
- img_path path to the image
- Returns:
- ndarray numpy array containing the image in a flat shape
- ndarray shape of the read image before flattening
- """
- img = np.array(imageio.imread(img_path), dtype=np.uint8) # Use imageio.v2 to suppress deprecation warning
- orig_shape = img.shape
- return img.flatten(), orig_shape
- def write_image(img_path, img_data, shape):
- """
- Writes an image to a path from a flat numpy array, using the shape provided.
- Args:
- img_path path were to save the image
- img_data numpy array containing the image (flat)
- shape shape of the image to be saved
- """
- img_data = np.reshape(img_data, shape)
- imwrite(img_path, img_data)
- def bytes2array(byte_data):
- """
- Converts byte data to a bit array (numpy array, dtype=np.uint8).
- Args:
- byte_data the byte data
- Returns:
- ndarray a numpy array of the single bits that composed the byte data
- """
- byte_array = np.frombuffer(byte_data, dtype=np.uint8)
- return np.unpackbits(byte_array)
- def array2bytes(bit_array):
- """
- Converts a bit array (numpy array, dtype=np.uint8) to byte data.
- Args:
- bit_array the bit array
- Returns:
- bytes the byte data
- """
- byte_array = np.packbits(bit_array)
- return byte_array.tobytes()
- def read_file(file_path):
- """
- Reads a file as a bit array (numpy array, dtype=np.uint8)
- Args:
- file_path path to the file
- Returns:
- ndarray the bit array
- """
- file_bytes = open(file_path, "rb").read()
- return bytes2array(file_bytes)
- def write_file(file_path, file_bit_array):
- """
- Writes a file to a path from a bit array (numpy array, dtype=np.uint8).
- Args:
- file_path path to the file
- file_bit_array the bit array of the file
- """
- bytes_data = array2bytes(file_bit_array)
- with open(file_path, 'wb') as f:
- f.write(bytes_data)
- def encode_data(image, file_data):
- """
- Encodes the file data onto the image
- Args:
- image the original image numpy array (flat)
- file_data the file data (bit array)
- Returns:
- ndarray the encoded image as a numpy array
- """
- 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):
- """
- Decodes the data from an image
- Args:
- encoded_data the encoded image as numpy array
- Returns:
- ndarray the bit array containing the file bits
- """
- out_mask = np.ones_like(encoded_data)
- output = np.bitwise_and(encoded_data, out_mask)
- return output
- def hide_images():
- original_file = original_entry_hide.get()
- hide_file = hide_entry_hide.get()
- save_file = save_entry_hide.get()
- img_path = f'DEEPFAKE/{original_file}.jpg'
- file_path = f'DEEPFAKE/{hide_file}.jpg'
- output_path = f'DATA/{save_file}.png'
- if not os.path.isfile(img_path):
- print("Image file does not exist")
- return
- if not os.path.isfile(file_path):
- print("File does not exist")
- return
- image, shape_orig = read_image(img_path)
- file = read_file(file_path)
- file_len = file.shape[0]
- len_array = np.array([file_len], dtype=np.uint32).view(np.uint8)
- len_array = np.unpackbits(len_array)
- img_len = image.shape[0]
- if file_len >= img_len - header_len: # 4 bytes are used to store file length
- print("File too big, error")
- return
- else: # Insert padding. Using random padding, otherwise values would all be even if padding with zeros (could be noticed in histogram).
- tmp = file
- file = np.random.randint(2, size=img_len, dtype=np.uint8)
- file[header_len:header_len+file_len] = tmp
- # file = np.pad(file, (header_len,img_len - file_len - header_len), 'constant', constant_values=(0, 0))
- file[:header_len] = len_array
- encoded_data = encode_data(image, file)
- write_image(output_path, encoded_data, shape_orig)
- print("Image encoded")
- # Preview encoded image
- #preview_img = Image.fromarray(encoded_data.reshape(shape_orig))
- #preview_img.show()
- def unhide_images():
- original_file = original_entry_unhide.get()
- save_file = save_entry_unhide.get()
- img_path = f'DATA/{original_file}.png'
- if not os.path.isfile(img_path):
- print("Image file does not exist")
- return
- encoded_data, shape_orig = read_image(img_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:header_len + extracted_len]
- # Ensure the reshaping operation matches the size of the array correctly
- expected_rows = extracted_len // 3
- if extracted_len % 3 != 0:
- expected_rows += 1
- data = np.pad(data, (0, 3 * expected_rows - extracted_len), 'constant') # Pad to ensure correct reshaping
- write_file(f'DATA/{save_file}.jpg', data)
- print("Image decoded")
- # Preview decoded image
- #preview_img = Image.fromarray(data.reshape((expected_rows, 3)))
- #preview_img.show()
- root = tk.Tk()
- root.geometry("490x160")
- root.resizable(False, False)
- root.configure(bg="#34495e")
- root.title("Najeeb Generator AI")
- #Label
- Label(root, text="NAJEEB AI IMAGES GENERATOR", bg="#34495e", fg="white", font="arial 22 bold").place(x=10, y=10)
- # Hide Frame
- hide_frame = tk.Frame(root, bg="#FFFF99")
- hide_frame.grid(row=0, column=0, padx=10, pady=50)
- tk.Label(hide_frame, text="Original Image:", bg="#FFFF99").grid(row=0, column=0)
- tk.Label(hide_frame, text="Bind Image:", bg="#FFFF99").grid(row=1, column=0)
- tk.Label(hide_frame, text="Save Image:", bg="#FFFF99").grid(row=2, column=0)
- original_entry_hide = tk.Entry(hide_frame)
- hide_entry_hide = tk.Entry(hide_frame)
- save_entry_hide = tk.Entry(hide_frame)
- original_entry_hide.grid(row=0, column=1)
- hide_entry_hide.grid(row=1, column=1)
- save_entry_hide.grid(row=2, column=1)
- hide_button = tk.Button(hide_frame, text="MAKE-AI-BIND", command=hide_images, bg="#FF6666", fg="white")
- hide_button.grid(row=3, columnspan=2)
- # Unhide Frame
- unhide_frame = tk.Frame(root, bg="#99FF99")
- unhide_frame.grid(row=0, column=1, padx=10, pady=50)
- tk.Label(unhide_frame, text="Both Side Enter Name Only:", bg="#99FF99").grid(row=0, column=1)
- tk.Label(unhide_frame, text="Bind AI Image:", bg="#99FF99").grid(row=1, column=0)
- tk.Label(unhide_frame, text="Save Encode:", bg="#99FF99").grid(row=2, column=0)
- original_entry_unhide = tk.Entry(unhide_frame)
- save_entry_unhide = tk.Entry(unhide_frame)
- original_entry_unhide.grid(row=1, column=1)
- save_entry_unhide.grid(row=2, column=1)
- unhide_button = tk.Button(unhide_frame, text="GENERATE AI IMAGE", command=unhide_images, bg="#6699FF", fg="white")
- unhide_button.grid(row=3, columnspan=2)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement