-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeformer.py
More file actions
467 lines (448 loc) · 20 KB
/
Copy pathdeformer.py
File metadata and controls
467 lines (448 loc) · 20 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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
# important inbuilt libraries
from PIL import Image, ImageOps
# class
class ImageDeformer:
deformOptions = ["Twist", "Double Twist", "Horizontal Split", "Vertical Split", "Multiply", "Add Layer", "Chess Like", "Half Mirror", "Four Mirror", "Sine Curve"] # editing options
subEditingTree = {
"Twist" : {},
"Double Twist" : {},
"Horizontal Split" : {
"Repetaion" : {"minVal" : 2, "maxVal" : 64, "currentPosition" : 4, "change" : 2}
},
"Vertical Split" : {
"Repetaion" : {"minVal" : 2, "maxVal" : 64, "currentPosition" : 4, "change" : 2}
},
"Multiply" : {
"Multiply factor" : {"minVal" : 0, "maxVal" : 6, "currentPosition" : 0, "change" : 1}
},
"Add Layer" : {
"Layer number" : {"minVal" : 1, "maxVal" : 10, "currentPosition" : 5, "change" : 1},
"Padding" : {"minVal" : 10, "maxVal" : 100, "currentPosition" : 10, "change" : 10}
},
"Chess Like" : {
"Box Length" : {"minVal" : 1, "maxVal" : 80, "currentPosition" : 20, "change" : 5}
},
"Half Mirror" : {},
"Four Mirror" : {},
"Sine Curve" : {
"Cycle value" : {"minVal" : 4, "maxVal" : 64, "currentPosition" : 32, "change" : 4}
}
}
# constants
layerValue = 5
paddingValue = 10
# provides user messege
def getMessage(self):
return self.userMessage
# get image object
def getImageObject(self, image : Image.Image) -> None:
self.image = image
return
# looks like iamge is fast forwarded
def middleTwist(self):
image = self.image # copying original image object
try:
# mesh class will parse to deform method
class DeformMesh:
def getmesh(self, image):
width , height = image.size
desiredMesh1 = (0,0, 0,height, width, 0, width, height)
targetMesh1 = (0, 0, width, height)
return [(targetMesh1, desiredMesh1)]
image = ImageOps.deform(image, DeformMesh())
except IOError as ioError:
return f"{ioError}"
except MemoryError as memoryError:
return f"Insufficient Error {memoryError}"
except ValueError as valueError:
return f"invalid value {valueError}"
self.image = image
return self.image
# twist at both quarter half
def doubleTwisted(self):
image = self.image # copying image object
try:
class DeformMesh:
def getmesh(self, image : Image.Image):
width , height = image.size
desiredMesh1 = (0, 0, 0, height, width/2,0, width/2, height)
targetMesh1 = (0, 0, width//2, height)
desiredMesh2 = (width/2, height, width/2, 0, width, height, width, 0)
targetMesh2 = (width//2, 0, width, height)
return [(targetMesh1, desiredMesh1), (targetMesh2, desiredMesh2)]
image = ImageOps.deform(image=image, deformer= DeformMesh())
except IOError as ioError:
return f"{ioError}"
except MemoryError as memoryError:
return f"Insufficient Error {memoryError}"
except ValueError as valueError:
return f"invalid value {valueError}"
self.image = image
return self.image
# horizontally devides and deviates the image dimentions
def horizontalSplit(self, repeaterValue = 4):
# copying actual image instance
image = self.image
try:
class DefromElmlecator:
def getmesh(self, image : Image.Image):
repeater = repeaterValue
width, height = image.size # original size of the image
width4, height8 = width//4, height//repeater # breaking the width and hight into quarters
width34 = width4*3 # 3rd quarter of the width
meshes = [] # stores the mesh co ordinates
for i in range(repeater):
firstQuarter= (0 if i%2 == 0 else width4)
thirdQuarter = (width34 if i%2 == 0 else width)
meshes.append(((0, i*height8,
width, (i+1) * height8),
(firstQuarter, i*height8,
firstQuarter, (i+1)*height8,
thirdQuarter, (i+1)*height8,
thirdQuarter, i* height8)))
return meshes
image = ImageOps.deform(image=image, deformer=DefromElmlecator())
except IOError as ioError:
return f"{ioError}"
except MemoryError as memoryError:
return f"Insufficient Error {memoryError}"
except ValueError as valueError:
return f"invalid value {valueError}"
except ZeroDivisionError as zeroDivisionError:
return f"Cant break into more segments {zeroDivisionError}"
self.image = image
return self.image
pass
# vertical split
def verticalSplit(self, repeaterValue = 4):
image = self.image # copyin the actual image obejct
try:
class DeformImplicator:
def getmesh(self, image : Image.Image):
width, height = image.size # copying actual image size
widthVar, height4 = width//repeaterValue, height//4
height34 = 3*height4
meshes = [] # stores the co ordiantes
for i in range(repeaterValue):
firstQuarter = (0 if i%2 == 0 else height4)
thirdQuarter = (height34 if i%2 ==0 else height)
meshes.append(
(
(
i*widthVar, 0,
(i+1)*widthVar, height
), # target rect
(
i*widthVar, firstQuarter,
i*widthVar, thirdQuarter,
(i+1)*widthVar, thirdQuarter,
(i+1)*widthVar, firstQuarter
) # source rect
)
)
return meshes
image = ImageOps.deform(image=image, deformer=DeformImplicator())
except IOError as ioError:
return f"{ioError}"
except MemoryError as memoryError:
return f"Insufficient Error {memoryError}"
except ValueError as valueError:
return f"invalid value {valueError}"
except ZeroDivisionError as zeroDivisionError:
return f"Cant break into more segments {zeroDivisionError}"
self.image = image
return self.image
def multiply(self, repitaionNumber = 2):
image = self.image # coping actual image
try:
# deformed class
class DeformImplicator:
def getmesh(self, img : Image.Image):
widthHalf, heightHalf = img.size[0]//2, img.size[1]//2
meshes = []
for i in range(2):
for j in range(2):
meshes.append(
(
(
i * widthHalf, j * heightHalf,
(i + 1) * widthHalf, (j + 1) * heightHalf
), # target rectangle
(
0, 0,
0, heightHalf * 2,
widthHalf * 2, heightHalf * 2,
widthHalf * 2, 0
) # source rectangle
)
)
return meshes
# main process
for _ in range(repitaionNumber):
image = ImageOps.deform(image=image, deformer= DeformImplicator())
except IOError as ioError:
return f"{ioError}"
except MemoryError as memoryError:
return f"Insufficient Error {memoryError}"
except ValueError as valueError:
return f"invalid value {valueError}"
except ZeroDivisionError as zeroDivisionError:
return f"Cant break into more segments {zeroDivisionError}"
self.image = image
return self.image
# infiinite layer
def layerize(self, repeaterValue = 0, padding = 0):
image = self.image # copying actual image object
# holding and releasing the repeater value
if repeaterValue != 0:
self.layerValue = repeaterValue
else:
repeaterValue = self.layerValue
#holding and releasing padding value
if padding != 0:
self.paddingValue = padding
else:
padding = self.paddingValue
try:
class DeformImplicator:
def getmesh(self, img : Image.Image):
width, height = img.size # image width, height
meshes = [] # holds co ordinates
for i in range(repeaterValue):
pad = padding * i
# if i > repeater-1:
# break
if width < 2 * pad or height < 2 * pad:
break
meshes.append(
(
(
pad, pad,
width - pad, height - pad
), # target rectangle
(
0, 0,
0, height,
width, height,
width, 0
) # source rectangle
)
)
return meshes
image = ImageOps.deform(image=image, deformer=DeformImplicator())
except IOError as ioError:
return f"{ioError}"
except MemoryError as memoryError:
return f"Insufficient Error {memoryError}"
except ValueError as valueError:
return f"invalid value {valueError}"
except ZeroDivisionError as zeroDivisionError:
return f"Cant break into more segments {zeroDivisionError}"
self.image = image
return self.image
# chekcer box pattern
def chess(self, boxGap = 20):
image = self.image # copying actual image instance
# procedure
try:
columns, rows = image.size[0]//boxGap, image.size[1]//boxGap # row and column
class DeformImplicator:
def getmesh(self, img : Image.Image):
meshes = []
for i in range(columns):
for j in range(rows):
if i % 2 != 0 or j % 2 != 0:
continue
meshes.append(
(
(
i * boxGap, j * boxGap,
(i+1) * boxGap, (j+1) * boxGap
), # target rectangle
(
i * boxGap, j * boxGap,
i * boxGap, (j + 1) * boxGap,
(i + 1) * boxGap, (j + 1) * boxGap,
(i+ 1) * boxGap, j * boxGap
) # source rectangle
)
)
return meshes
image = ImageOps.deform(image=image, deformer= DeformImplicator())
except IOError as ioError:
return f"{ioError}"
except MemoryError as memoryError:
return f"Insufficient Error {memoryError}"
except ValueError as valueError:
return f"invalid value {valueError}"
except ZeroDivisionError as zeroDivisionError:
return f"Cant break into more segments {zeroDivisionError}"
self.image = image
return self.image
# mirror pattern
def mirrorHalf(self)-> str:
image = self.image # coping actual image instance
try:
class DeformImplicator:
def getmesh(self, img : Image.Image):
width , height = img.size # weight and height
widthHalf , heightHalf = width//2, height//2 # half of width and height
meshes = [
(
(
0,0,
widthHalf, height
), # target rectangle
(
0, 0,
0, height,
widthHalf, height,
widthHalf, 0
)
),
(
(
widthHalf, 0,
width, height
), # target rectangle
(
widthHalf, 0,
widthHalf, height,
0, height,
0, 0
) # source rectangle
)
]
return meshes
image = ImageOps.deform(image=image, deformer=DeformImplicator())
except IOError as ioError:
return f"{ioError}"
except MemoryError as memoryError:
return f"Insufficient Error {memoryError}"
except ValueError as valueError:
return f"invalid value {valueError}"
except ZeroDivisionError as zeroDivisionError:
return f"Cant break into more segments {zeroDivisionError}"
self.image = image
return self.image
# mirrorification of the first quardant in 4 parts
def mirrorQuad(self):
image = self.image # coping actual image instance
try:
class DeformImplicator:
def getmesh(self, img : Image.Image):
width, height = img.size
quarterWidth, quarerHeight = width//2, height//2 # quarter half of the width, height
meshes = [
(
(
0, 0,
quarterWidth, quarerHeight
), # target rectangle
(
0, 0,
0, quarerHeight,
quarterWidth, quarerHeight,
quarterWidth, 0
) # source rectangle
), # first quardant
(
(
quarterWidth, 0,
width, quarerHeight
), # target rectangle
(
quarterWidth, 0,
quarterWidth, quarerHeight,
0, quarerHeight,
0, 0
) # source rectangle
), # second quarter
(
(
0, quarerHeight,
quarterWidth, height
), # target rectangle
(
0, quarerHeight,
0, 0,
quarterWidth, 0,
quarterWidth, quarerHeight
) # source rectangle
), # third quarter
(
(
quarterWidth, quarerHeight,
width, height
), # target rectangle
(
quarterWidth, quarerHeight,
quarterWidth, 0,
0, 0,
0, quarerHeight
) # source rectangle
) # fourth quarter
]
return meshes
image = ImageOps.deform(image = image, deformer = DeformImplicator())
except IOError as ioError:
return f"{ioError}"
except MemoryError as memoryError:
return f"Insufficient Error {memoryError}"
except ValueError as valueError:
return f"invalid value {valueError}"
except ZeroDivisionError as zeroDivisionError:
return f"Cant break into more segments {zeroDivisionError}"
self.image = image
return self.image
# a sinosoidal curve is generated
def sinCurve(self, cycle = 32):
image = self.image # copying image object
halfCycle, quarterHalfCycle = cycle//2, cycle //4
# procedure
try:
image = self.image
class deformImplicator:
def getmesh(self, img : Image.Image):
width, height = img.size # width and height of the image
# width part creates column and heightPart increase the unit height in iteration
widthPart , increamentFactor = width//cycle, height//halfCycle
# in height = initial height and end height = final height
inheight, endHeight = (quarterHalfCycle-1) * increamentFactor, (halfCycle-1) * increamentFactor
meshes = [] # holds the co ordinates
for i in range(cycle + 1):
inheight = inheight + increamentFactor # updates initial height value
endHeight = endHeight + increamentFactor # updates final height value
meshes.append(
(
(
i * widthPart, inheight,
(i + 1)* widthPart, endHeight
), # targetRectangle
(
i * widthPart, inheight,
i * widthPart, endHeight,
(i + 1) * widthPart, endHeight,
(i + 1) * widthPart, inheight
) # rource rectangle
)
)
if (i % quarterHalfCycle) == 0 :
increamentFactor = - increamentFactor
return meshes
image = ImageOps.deform(image= image, deformer= deformImplicator())
except IOError as ioError:
return f"{ioError}"
except MemoryError as memoryError:
return f"Insufficient Error {memoryError}"
except ValueError as valueError:
return f"invalid value {valueError}"
except ZeroDivisionError as zeroDivisionError:
return f"Cant break into more segments {zeroDivisionError}"
self.image = image
return self.image
pass
if __name__ == '__main__':
imd = ImageDeformer(R"D:\Gallery\PhotoSpace\downloaded pic\wp13349909-aquaman-and-the-lost-kingdom-wallpapers.jpg")
print(imd.sinCurve())
imd.image.show()