Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
Image encryption and decryption
  • Loading branch information
Vatsan2004 authored Aug 26, 2024
1 parent fd4fe94 commit f49151e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
Binary file added decrypted_image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added encrypted_image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
54 changes: 54 additions & 0 deletions task2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from PIL import Image
import random

def encrypt_image(image_path, output_path):
try:
image = Image.open(image_path)
pixels = image.load()
width, height = image.size

# Apply a simple pixel shift for encryption
for i in range(width):
for j in range(height):
r, g, b = pixels[i, j]
# Simple transformation: shift RGB values
r = (r + 50) % 256
g = (g + 100) % 256
b = (b + 150) % 256
pixels[i, j] = (r, g, b)

image.save(output_path)
print(f"Image encrypted and saved as {output_path}")

except FileNotFoundError:
print(f"File not found: {image_path}. Please check the file path and try again.")

def decrypt_image(encrypted_path, output_path):
try:
image = Image.open(encrypted_path)
pixels = image.load()
width, height = image.size

# Reverse the simple pixel shift for decryption
for i in range(width):
for j in range(height):
r, g, b = pixels[i, j]
r = (r - 50) % 256
g = (g - 100) % 256
b = (b - 150) % 256
pixels[i, j] = (r, g, b)

image.save(output_path)
print(f"Image decrypted and saved as {output_path}")

except FileNotFoundError:
print(f"File not found: {encrypted_path}. Please check the file path and try again.")

# Use paths for testing
image_path = r'C:\Users\welcome\Downloads\images.jpg'
encrypted_image_path = r'C:\Users\welcome\Downloads\encrypted_image.png'
decrypted_image_path = r'C:\Users\welcome\Downloads\decrypted_image.png'

# Encrypt and decrypt the image
encrypt_image(image_path, encrypted_image_path)
decrypt_image(encrypted_image_path, decrypted_image_path)

0 comments on commit f49151e

Please sign in to comment.