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 PIL import Image, ImageTk
- import subprocess
- 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 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:
- print("File too big, error")
- return
- else:
- tmp = file
- file = np.random.randint(2, size=img_len, dtype=np.uint8)
- file[header_len:header_len+file_len] = tmp
- file[:header_len] = len_array
- encoded_data = encode_data(image, file)
- write_image(output_path, encoded_data, shape_orig)
- print("Image encoded")
- preview_img = Image.fromarray(encoded_data.reshape(shape_orig))
- photo = ImageTk.PhotoImage(preview_img)
- lbl.config(image=photo)
- lbl.image = photo
- 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:extracted_len+header_len]
- write_file(f'DATA/{save_file}.jpg', data)
- print("Image decoded and saved")
- saved_image_path = f'DATA/{save_file}.jpg'
- #subprocess.Popen(['start', saved_image_path], shell=True)
- # Load the saved image for display in Frame2
- saved_image = Image.open(saved_image_path)
- #saved_image.thumbnail((shape_orig[0] // 3, shape_orig[1])) # Resize if needed
- photo = ImageTk.PhotoImage(saved_image)
- lbl2.config(image=photo)
- lbl2.image = photo
- root = tk.Tk()
- root.geometry("1000x660")
- root.title("Hide and Unhide Images")
- # Frame for Original Image
- f = tk.Frame(root, bd=3, bg="#2c3e50", width=500, height=630, relief=tk.GROOVE)
- f.place(x=5, y=10)
- tk.Label(f, text="Open Original Image:").place(x=10, y=10)
- original_entry_hide = tk.Entry(f)
- original_entry_hide.place(x=150, y=10)
- tk.Label(f, text="Open Image to Hide:").place(x=10, y=40)
- hide_entry_hide = tk.Entry(f)
- hide_entry_hide.place(x=150, y=40)
- tk.Label(f, text="Save Hidden Image:").place(x=10, y=70)
- save_entry_hide = tk.Entry(f)
- save_entry_hide.place(x=150, y=70)
- hide_button = tk.Button(f, text="Hide", command=hide_images)
- hide_button.place(x=150, y=100)
- lbl = tk.Label(f, bg="#2c3e50")
- lbl.place(x=10, y=140)
- # Frame for Unhidden Image
- frame2 = tk.Frame(root, bd=3, width=500, height=630, relief=tk.GROOVE, bg="#34495e")
- frame2.place(x=505, y=10)
- tk.Label(frame2, text="Open Encoded Image:").place(x=10, y=10)
- original_entry_unhide = tk.Entry(frame2)
- original_entry_unhide.place(x=150, y=10)
- tk.Label(frame2, text="Save Unhidden Image:").place(x=10, y=40)
- save_entry_unhide = tk.Entry(frame2)
- save_entry_unhide.place(x=150, y=40)
- unhide_button = tk.Button(frame2, text="Unhide", command=unhide_images)
- unhide_button.place(x=150, 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