-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.py
More file actions
27 lines (24 loc) · 1.11 KB
/
index.py
File metadata and controls
27 lines (24 loc) · 1.11 KB
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
from PIL import Image
import numpy as np
def encrypt_image(input_image_path, output_image_path, key):
img = Image.open(input_image_path)
img_array = np.array(img)
encrypted_array = np.bitwise_xor(img_array, key)
encrypted_img = Image.fromarray(encrypted_array)
encrypted_img.save(output_image_path)
print("Image encrypted successfully.")
return encrypted_array
def decrypt_image(encrypted_array, output_image_path, key):
decrypted_array = np.bitwise_xor(encrypted_array, key)
decrypted_img = Image.fromarray(decrypted_array)
decrypted_img.save(output_image_path)
print("Image decrypted successfully.")
if __name__ == "__main__":
input_image_path = r"original image.jpeg"
img = Image.open(input_image_path)
width, height = img.size
key = np.random.randint(0, 256, size=(height, width, 3), dtype=np.uint8) # 3 for RGB channels
encrypted_image_path = 'encrypted_image.jpg'
encrypted_array = encrypt_image(input_image_path, encrypted_image_path, key)
decrypted_image_path = 'decrypted_image.jpg'
decrypt_image(encrypted_array, decrypted_image_path, key)