0% found this document useful (0 votes)
2 views

Assignment_Sarinchandevid.ipynb - Colab

The document outlines a coding assignment by Sarin Chandevid that involves uploading two images using Google Colab, resizing them, and then encrypting one image using the other with an XOR operation. It includes steps for displaying the images, performing encryption and decryption, and saving the results. The code utilizes the PIL library for image manipulation and demonstrates basic image processing techniques in Python.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Assignment_Sarinchandevid.ipynb - Colab

The document outlines a coding assignment by Sarin Chandevid that involves uploading two images using Google Colab, resizing them, and then encrypting one image using the other with an XOR operation. It includes steps for displaying the images, performing encryption and decryption, and saving the results. The code utilizes the PIL library for image manipulation and demonstrates basic image processing techniques in Python.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

11/6/24, 12:15 AM Assignment_Sarinchandevid.

ipynb - Colab

Name: Sarin Chandevid Assignment Security and Network

# prompt: write a code to upload two images

from google.colab import files

uploaded = files.upload()

for fn in uploaded.keys():
print('User uploaded file "{name}" with length {length} bytes'.format(
name=fn, length=len(uploaded[fn])))

# You can now access the uploaded files using the file names as keys in the `uploaded` dictionary.
# For example:
# image1 = uploaded['image1.jpg']
# image2 = uploaded['image2.png']

Choose Files No file chosen Upload widget is only available when the cell has been executed in the current browser session. Please rerun this cell to
enable.
Saving photo_2024-11-05_23-44-25.jpg to photo_2024-11-05_23-44-25 (7).jpg
Saving photo_2024-11-05_23-44-26.jpg to photo_2024-11-05_23-44-26 (10).jpg
User uploaded file "photo_2024-11-05_23-44-25 (7).jpg" with length 310040 bytes
User uploaded file "photo 2024-11-05 23-44-26 (10).jpg" with length 105480 bytes

# prompt: make he size of those two images smaller and then show the two images photo_2024-11-05_23-44-25 (7).jpg and photo_2024-11-05_23-44-2

from PIL import Image


import io

# Assuming you have uploaded the images and they are in the `uploaded` dictionary
image1_data = uploaded['photo_2024-11-05_23-44-25 (7).jpg']
image2_data = uploaded['photo_2024-11-05_23-44-26 (10).jpg']

# Open the images using PIL


image1 = Image.open(io.BytesIO(image1_data))
image2 = Image.open(io.BytesIO(image2_data))

# Resize the images


new_width = 300 # Adjust as needed
new_height = 200 # Adjust as needed
image1 = image1.resize((new_width, new_height))
image2 = image2.resize((new_width, new_height))

# Display the images


display(image1)
display(image2)

# prompt: use the small size images of photo_2024-11-05_23-44-25 (7).jpg as key image to encrypt photo_2024-11-05_23-44-26 (10).jpg . Use XO

# Convert images to pixel arrays


image1_pixels = list(image1.getdata())
image2_pixels = list(image2.getdata())

https://colab.research.google.com/drive/1eCHI8sVvCFY8T6MmseKPbzcMhSyBFMpI#scrollTo=Ltju9qeBTEw4&printMode=true 1/3
11/6/24, 12:15 AM Assignment_Sarinchandevid.ipynb - Colab

# Perform XOR operation on pixel values


encrypted_pixels = [tuple(p1 ^ p2 for p1, p2 in zip(pixel1, pixel2))
for pixel1, pixel2 in zip(image1_pixels, image2_pixels)]

# Create a new image with the encrypted pixels


encrypted_image = Image.new(image2.mode, image2.size)
encrypted_image.putdata(encrypted_pixels)

# Display the encrypted image


display(encrypted_image)

# Optionally, save the encrypted image


# encrypted_image.save('encrypted_image.jpg')

# prompt: decrypt back the image using the key image

# Assuming you have the encrypted image and the key image loaded as 'encrypted_image' and 'image2'

# Convert the encrypted image and key image to pixel arrays


encrypted_pixels = list(encrypted_image.getdata())
key_pixels = list(image2.getdata())

# Perform XOR operation again to decrypt the image


decrypted_pixels = [tuple(p1 ^ p2 for p1, p2 in zip(pixel1, pixel2))
for pixel1, pixel2 in zip(encrypted_pixels, key_pixels)]

# Create a new image with the decrypted pixels


decrypted_image = Image.new(encrypted_image.mode, encrypted_image.size)
decrypted_image.putdata(decrypted_pixels)

# Display the decrypted image


display(decrypted_image)

# Optionally, save the decrypted image


# decrypted_image.save('decrypted_image.jpg')

https://colab.research.google.com/drive/1eCHI8sVvCFY8T6MmseKPbzcMhSyBFMpI#scrollTo=Ltju9qeBTEw4&printMode=true 2/3
11/6/24, 12:15 AM Assignment_Sarinchandevid.ipynb - Colab

https://colab.research.google.com/drive/1eCHI8sVvCFY8T6MmseKPbzcMhSyBFMpI#scrollTo=Ltju9qeBTEw4&printMode=true 3/3

You might also like