-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHenonMap.py
executable file
·148 lines (116 loc) · 3.82 KB
/
HenonMap.py
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
from PIL import Image
import numpy as np
import os
from matplotlib.pyplot import imshow
import matplotlib.pyplot as plt
import cv2
import random
from math import log
def readImage(imageName):
temp_image = Image.open(imageName)
imagePixelData = temp_image.load()
imagems = temp_image.size
imagePixelValues = []
color = 1
if type(imagePixelData[0,0]) == int:
color = 0
for i in range(int(imagems[0])):
row = []
for j in range(int(imagems[1])):
row.append((imagePixelData[i,j]))
imagePixelValues.append(row)
return imagePixelValues, imagems[0], imagems[1],color
def CalculateTotalBitValue(bitSequence):
value = 0
for bit in bitSequence:
value = value * 2 + int(bit)
return value
def genHenonMap(m, key):
sequenceSize = m * m * 8
bitSequence = []
byteArray = []
TransformMatrixValues = []
x = key[0]
y = key[1]
for i in range(sequenceSize):
xN = y + 1 - 1.4 * x**2
yN = 0.3 * x
x = xN
y = yN
if xN <= 0.4:
bit = 0
else:
bit = 1
bitSequence.append(bit)
if i % 8 == 7:
decimal = CalculateTotalBitValue(bitSequence)
byteArray.append(decimal)
bitSequence = []
byteArraySize = m*8
if i % byteArraySize == byteArraySize-1:
TransformMatrixValues.append(byteArray)
byteArray = []
return TransformMatrixValues
def HenonEncryption(imageName,key):
imageimagePixelDataelData, m, n, color = readImage(imageName)
OutPutImage = []
HenonMapMatrix = genHenonMap(m, key)
for i in range(m):
li = []
for j in range(n):
if color:
#print("here: ",tuple([HenonMapMatrix[i][j] ^ x for x in imageimagePixelDataelData[i][j]]))
li.append(tuple([HenonMapMatrix[i][j] ^ x for x in imageimagePixelDataelData[i][j]]))
else:
li.append(HenonMapMatrix[i][j] ^ imageimagePixelDataelData[i][j])
OutPutImage.append(li)
if color:
temp_image = Image.new("RGB", (m, n))
else:
temp_image = Image.new("L", (m, n))
imagePixelData = temp_image.load()
for x in range(m):
for y in range(n):
imagePixelData[x, y] = OutPutImage[x][y]
temp_image.save(imageName.split('.')[0] + "_HenonEnc.png", "PNG")
def HenonDecryption(imageNameEnc, key):
imageimagePixelDataelData, m, n, color = readImage(imageNameEnc)
DecryptedImage = []
HenonMapMatrix = genHenonMap(m, key)
for i in range(m):
li = []
for j in range(n):
if color:
li.append(tuple([HenonMapMatrix[i][j] ^ x for x in imageimagePixelDataelData[i][j]]))
else:
li.append(HenonMapMatrix[i][j] ^ imageimagePixelDataelData[i][j])
DecryptedImage.append(li)
if color:
temp_image = Image.new("RGB", (m, n))
else:
temp_image = Image.new("L", (m, n)) # L is for Black and white imagePixelDataels
imagePixelData = temp_image.load()
for x in range(m):
for y in range(n):
imagePixelData[x, y] = DecryptedImage[x][y]
temp_image.save(imageNameEnc.split('_')[0] + "_HenonDec.png", "PNG")
cwd_path = os.getcwd()
print("path: ", cwd_path)
image_path = cwd_path + '/test_copy.png'
image = "test_copy"
ext = ".png"
key = (0.1,0.1)
clr_img = Image.open(image_path, 'r')
imshow(np.asarray(clr_img))
plt.show()
img_data = clr_img.load()
print(type(img_data))
print(clr_img.size)
HenonEncryption(image_path, key)
temp_image = Image.open(image + "_HenonEnc.png", 'r')
imshow(np.asarray(temp_image))
plt.show()
HenonDecryption(image + "_HenonEnc.png", key)
temp_image = Image.open(image + "_HenonDec.png", 'r')
imshow(np.asarray(temp_image))
plt.show()