-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathimage_managing.py
232 lines (188 loc) · 6.65 KB
/
image_managing.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
from PIL import Image
import numpy as np
import math
from utils import imodule, coprime
import sys
#from watermark_embedding_extraction import LSB
#Return numpy array from a Image file
def loadImage(path=""):
if path is "":
sys.exit("LOAD IMAGE: Path must not be None!")
img = Image.open(path)
return img
#Show image from array or path
def showImage(img):
if type(img) is not str:
img.show()
else:
if img is "":
sys.exit("SHOW IMAGE: Path must not be None!")
Image.open(img).show()
#Save image from array or Image file
def saveImage(img, path):
if path is "":
sys.exit("SHOW IMAGE: Path must not be None!")
img.save(path)
def grayscale(img):
return img.convert(mode="L")
#Return binarized image
def binarization(img):
return img.convert(mode="1",dither=0)
#Return image size
def imgSize(img):
if type(img) is np.ndarray:
width, heigth = (img.shape[1], img.shape[0])
else:
width, heigth = img.size
return width, heigth
#Arnold transform
def arnoldTransform(img, iteration):
width, heigth = imgSize(img)
if width != heigth:
sys.exit("ARNOLD TRANSFORM: Image must be square!")
side = width
toTransform = img.copy()
transformed = img.copy()
for iter in range(iteration):
for i in range(side):
for j in range(side):
newX = (i + j) % side
newY = (i + 2*j) % side
value = toTransform.getpixel(xy=(i,j))
transformed.putpixel(xy=(newX,newY),value=value)
toTransform = transformed.copy()
return transformed
#Inverse Arnold transform
def iarnoldTransform(img, iteration):
width, heigth = imgSize(img)
if width != heigth:
sys.exit("IARNOLD TRANSFORM: Image must be square!")
side = width
transformed = img.copy()
toTransform = img.copy()
for iter in range(iteration):
for i in range(side):
for j in range(side):
newX = (2*i - j) % side
newY = (-i + j) % side
value = toTransform.getpixel(xy=(i,j))
transformed.putpixel(xy=(newX,newY),value=value)
toTransform = transformed.copy()
return transformed
#2D lower triangular mapping
def lowerTriangularMappingTransform(img, iteration, c, a=-1, d=-1):
width, heigth = imgSize(img)
coprime_mode = "first"
if a == -1 and d == -1:
a = coprime(width, coprime_mode)
d = coprime(heigth, coprime_mode)
transformed = img.copy()
toTransform = img.copy()
for iter in range(iteration):
for i in range(width):
for j in range(heigth):
newX = (a*i) % width
newY = (c*i + d*j) % heigth
value = toTransform.getpixel(xy=(i,j))
transformed.putpixel(xy=(newX,newY),value=value)
toTransform = transformed.copy()
return transformed
#2D inverse lower triangular mapping
def ilowerTriangularMappingTransform(img, iteration, c, a=-1, d=-1):
width, heigth = imgSize(img)
coprime_mode = "first"
if a == -1 and d == -1:
a = coprime(width, coprime_mode)
d = coprime(heigth, coprime_mode)
transformed = img.copy()
toTransform = img.copy()
ia = imodule(a, width)
id = imodule(d, heigth)
for iter in range(iteration):
for i in range(width):
for j in range(heigth):
newX = (ia*i) % width
newY = (id*(j + (math.ceil(c*width/heigth)*heigth) - (c*newX))) % heigth
value = toTransform.getpixel(xy=(i,j))
transformed.putpixel(xy=(newX,newY),value=value)
toTransform = transformed.copy()
return transformed
#2D upper triangular mapping
def upperTriangularMappingTransform(img, iteration, c, a=-1, d=-1):
width, heigth = imgSize(img)
coprime_mode = "first"
if a == -1 and d == -1:
a = coprime(width, coprime_mode)
d = coprime(heigth, coprime_mode)
transformed = img.copy()
toTransform = img.copy()
for iter in range(iteration):
for i in range(width):
for j in range(heigth):
newX = (a*i + c*j) % width
newY = (d*j) % heigth
value = toTransform.getpixel(xy=(i,j))
transformed.putpixel(xy=(newX,newY),value=value)
toTransform = transformed.copy()
return transformed
#2D inverse upper triangular mapping
def iupperTriangularMappingTransform(img, iteration, c, a=-1, d=-1):
width, heigth = imgSize(img)
coprime_mode = "first"
if a == -1 and d == -1:
a = coprime(width, coprime_mode)
d = coprime(heigth, coprime_mode)
transformed = img.copy()
toTransform = img.copy()
ia = imodule(a, width)
id = imodule(d, heigth)
for iter in range(iteration):
for i in range(width):
for j in range(heigth):
newY = (id*j) % heigth
newX = (ia*(i + (math.ceil(c*heigth/width)*width) - (c*newY))) % width
value = toTransform.getpixel(xy=(i,j))
transformed.putpixel(xy=(newX,newY),value=value)
toTransform = transformed.copy()
return transformed
def mappingTransform(mode, img, iteration, c, a=-1, d=-1):
if mode is "lower":
m = lowerTriangularMappingTransform(img,iteration,c,a,d)
return m
if mode is "upper":
m = upperTriangularMappingTransform(img,iteration,c,a,d)
return m
else:
sys.exit("MAPPING TRANSFORM: Mode must be lower or upper!")
def imappingTransform(mode, img, iteration, c, a=-1, d=-1):
if mode is "lower":
m = ilowerTriangularMappingTransform(img,iteration,c,a,d)
return m
if mode is "upper":
m = iupperTriangularMappingTransform(img,iteration,c,a,d)
return m
else:
sys.exit("MAPPING TRANSFORM: Mode must be lower or upper!")
'''
TESTING
'''
if __name__ == "__main__":
img = loadImage("right.png")
imgr = loadImage("07.jpg")
imgr = binarization(imgr)
"""
t = arnoldTransform(img,iteration=1)
showImage(t)
it = iarnoldTransform(t,iteration=1)
showImage(it)
m = mappingTransform(mode="lower",img=imgr,iteration=1,c=3,a=5)
showImage(m)
#saveImage(m, "triangular_2_iterations.png")
im = imappingTransform(mode="lower",img=m,iteration=1,c=3,a=5)
showImage(im)
m1 = mappingTransform(mode="upper",img=imgr,iteration=1,c=3)
showImage(m1)
#saveImage(m, "triangular_2_iterations.png")
im1 = imappingTransform(mode="upper",img=m1,iteration=1,c=3)
showImage(im1)
"""