Spaces:
Sleeping
Sleeping
Create embbed.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839import osimport numpy as npfrom PIL import Imagefrom 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")embbed.pyAdded+39