-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython program.py
More file actions
73 lines (60 loc) · 2.12 KB
/
Python program.py
File metadata and controls
73 lines (60 loc) · 2.12 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# -*- coding: utf-8 -*-
"""Encryption & Decryption
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1_2W9-AGesIiGsa5IynWemzBKQgR6KKFS
"""
#CPT 115 MATHEMATICAL METHODS FOR COMPUTER SCIENCE
#Assignment 1
#Group 4
# coding=gbk
from PIL import Image
import numpy as np
# import scipy
import matplotlib.pyplot as plt
# ImageToMatrix function definition
def ImageToMatrix(filename):
# read the image
im = Image.open(filename) #read the image
display(im) #display the image
# show the image
# im.show()
width,height = im.size #get the size of image
im = im.convert("L")
data = im.getdata()
data = np.matrix(data,dtype='float') #convert image to matrix
new_data = np.reshape(data,(height,width)) #reshape the size of matrix
return new_data
filename = '/2020-10-09 (1).png'
im = ImageToMatrix(filename) #convert image to matrix
# print detail of image
print()
print('Type of image: ',type(im))
print('Shape of the image:{}'.format(im.shape))
print('Image Height: {}'.format(im.shape[0]))
print('Image width: {}'.format(im.shape[1]))
print('Dimension of Image: {}'.format(im.ndim))
print('Image size: {}'.format(im.size))
print()
# generate a key in matrix form
key=np.random.random_integers(10,size=(im.shape[1],im.shape[1]))
# key = np.matrix(key,dtype='float')
inverse_key=np.linalg.inv(key) # inverse the key
# "encrypt" store the matrix of encrypted image
encrypt = im * key # multiply the image's matrix with key(matrix)
# "encrypt_im" is the encrypted image
# convert encrypted image's matrix into encrypted image
encrypt_im = Image.fromarray(encrypt.astype(np.uint8))
# print dectypted image
print()
print('Display encrypted image:')
display(encrypt_im)
# "decrypt" is the matrix of decrypted image
# decrypt the image by multiplying the encrypted image's matrix with the inverse of key(matrix)
decrypt = encrypt * inverse_key
# convert the matrix of decrypted image into image
decrypt_im = Image.fromarray(decrypt.astype(np.uint8))
# display the decrypted image
print()
print('display decrypted image:')
display(decrypt_im)