-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPixel_manipulation.py
More file actions
41 lines (30 loc) · 1.19 KB
/
Copy pathPixel_manipulation.py
File metadata and controls
41 lines (30 loc) · 1.19 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
28
29
30
31
32
33
34
35
36
37
38
39
import numpy as np
import cv2
def process_image(image_path, key, output_path):
"""
Encrypts or decrypts an image using XOR pixel manipulation.
Parameters:
image_path (str): Path to the image file.
key (int): Encryption key (must be the same for encryption and decryption).
output_path (str): Path to save the processed image.
"""
# Read the image
image = cv2.imread(image_path, cv2.IMREAD_COLOR)
if image is None:
raise ValueError("Image not found or cannot be read.")
# Convert image to a NumPy array
image_array = np.array(image, dtype=np.uint8)
# Apply XOR operation with the key to each pixel
processed_array = image_array ^ key
# Save the processed image
cv2.imwrite(output_path, processed_array)
print(f"Processed image saved as {output_path}")
return output_path
# Get user input
image_path = input("Enter the image file path: ")
key = int(input("Enter the encryption/decryption key (integer): "))
mode = input("Enter mode (encrypt/decrypt): ").strip().lower()
# Define output file name
output_path = "encrypted.png" if mode == "encrypt" else "decrypted.png"
# Process image
process_image(image_path, key, output_path)