-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pixel manipulation
53 lines (44 loc) · 1.75 KB
/
Pixel manipulation
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
52
53
from PIL import Image
#trying the encryption function
def encrypt_image(image):
#converting the image to RGB mode
image=image.convert('RGB')
#getting the pixel data
pixels=image.load()
#encrypting the image by adding a constant value to each pixel
for x in range(image.width):
for y in range(image.height):
r,g,b=pixels[x,y]
#adding 100 to each pixel value
pixels[x,y]=((r+100)%256, (g+100)%256, (b+100)%256)
return image
#trying the decryption function
def decrypt_image(image):
#converting the image to RGB mode
image=image.convert('RGB')
#getting the pixel data
pixels=image.load()
#decrypting the image by subtracting same constant value from each pixel
for x in range(image.width):
for y in range(image.height):
r, g, b = pixels[x, y]
#subtracting 100 from each pixel
pixels[x, y] = ((r-100)%256, (g-50)%256, (b-50)%256)
return image
user_choice=input("Would you like to encrypt or decrypt the image? Type 'encrypt' or 'decrypt': ").lower()
input_image_path = 'input.png' # Assuming the input image is named input.png
input_image = Image.open(input_image_path)
if user_choice=='encrypt':
output_image_path='encrypted_image.png'
output_image = encrypt_image(input_image)
action = 'encrypted'
elif user_choice == 'decrypt':
output_image_path = 'decrypted_image.png'
output_image = decrypt_image(input_image)
action = 'decrypted'
else:
print("Invalid choice. Please choose 'encrypt' or 'decrypt'.")
exit()
#saving the output image
output_image.save(output_image_path)
print(f"The image has been {action}! You can find it saved as '{output_image_path}'.")