Spaces:
Sleeping
Sleeping
File size: 1,245 Bytes
fd5af90 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | import os
import numpy as np
from PIL import Image
from cryptography.fernet import Fernet
def embed_data(encrypted_image_path, secret_image_path):
# Load images
enc_img = Image.open(encrypted_image_path).convert("L")
sec_img = Image.open(secret_image_path).convert("L")
enc_arr = np.array(enc_img)
sec_arr = np.array(sec_img)
# Flatten secret data
secret_data = sec_arr.flatten().astype(np.uint8)
secret_bin = ''.join(format(x, '08b') for x in secret_data)
# Shuffle key for secret
key3 = Fernet.generate_key()
with open("out/keys.txt", "a") as f:
f.write(key3.decode() + "\n")
print("Key for block shuffling: ", key3)
# Embed into encrypted image LSB
h, w = enc_arr.shape
flat = enc_arr.flatten()
data_len = len(secret_bin)
print("Length of secret data: ", data_len)
bin_len = format(data_len, '016b')
payload = bin_len + secret_bin
payload = payload[:len(flat)]
new_flat = flat.copy()
for i, bit in enumerate(payload):
new_flat[i] = (new_flat[i] & ~1) | int(bit)
new_arr = new_flat.reshape((h, w))
Image.fromarray(new_arr).save("reembbedded_image.png")
print("Blocks after embedding are saved to reembbedded_image.png")
|