-
Notifications
You must be signed in to change notification settings - Fork 0
/
image encryption tool.py
51 lines (44 loc) · 1.92 KB
/
image encryption tool.py
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
41
42
43
44
45
46
47
48
49
50
51
from PIL import Image
def encrypt_image(image_path, key, output_path):
"""
Encrypts an image by manipulating its pixel values.
Args:
- image_path (str): The path to the input image.
- key (int): The encryption key (integer).
- output_path (str): The path to save the encrypted image.
"""
# Open the image
img = Image.open(image_path)
pixels = img.load() # Load the pixel data
for i in range(img.size[0]): # For every pixel in the width
for j in range(img.size[1]): # For every pixel in the height
r, g, b = pixels[i, j]
# Encrypt each pixel by adding the key (simple encryption)
pixels[i, j] = (r + key) % 256, (g + key) % 256, (b + key) % 256
# Save the encrypted image
img.save(output_path)
print(f"Image encrypted and saved to {output_path}")
def decrypt_image(image_path, key, output_path):
"""
Decrypts an image by reversing the pixel value manipulation.
Args:
- image_path (str): The path to the encrypted image.
- key (int): The encryption key (integer).
- output_path (str): The path to save the decrypted image.
"""
# Open the encrypted image
img = Image.open(image_path)
pixels = img.load() # Load the pixel data
for i in range(img.size[0]): # For every pixel in the width
for j in range(img.size[1]): # For every pixel in the height
r, g, b = pixels[i, j]
# Decrypt each pixel by subtracting the key
pixels[i, j] = (r - key) % 256, (g - key) % 256, (b - key) % 256
# Save the decrypted image
img.save(output_path)
print(f"Image decrypted and saved to {output_path}")
if __name__ == "__main__":
# Example usage
key = 123 # Example key for encryption/decryption
encrypt_image("input.jpg", key, "encrypted.png")
decrypt_image("encrypted.png", key, "decrypted.png")