This repository has been archived by the owner on Jul 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
IImage.py
144 lines (122 loc) · 3.61 KB
/
IImage.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
from PIL import Image, ImageDraw
class IImage(object):
def __init__(self, arg):
if type(arg) == type('path'):
self.pil = Image.open(arg, 'r')
elif type(arg) == type(('size', 'tuple')):
self.pil = Image.new('RGBA', arg, 'black')
else:
self.pil = arg
self.width, self.height = self.pil.size
def save(self, path, extension='JPEG', quality=80):
self.pil.save(path, extension, quality=quality)
return self
def flipH(self):
data = (self.width, 0, 0, self.height)
self.pil = self.pil.transform((self.width,self.height), Image.EXTENT, data)
return self
def flipV(self):
data = (0, self.height, self.width, 0)
self.pil = self.pil.transform((self.width,self.height), Image.EXTENT, data)
return self
def flipHV(self):
return self.flipH().flipV()
def flipVH(self):
return self.flipV().flipH()
def duplicateMirrorV(self):
canvas = IImage((self.width*2, self.height))
return canvas.paste(self, 0, 0).paste(self.flipV(), self.width, 0)
def rotate(self, angle):
self.pil = self.pil.rotate(angle)
return self
def paste(self, iimg, x, y):
self.pil.paste(iimg.pil, (x,y))
return self
def thumbnail(self, width, height, mode):
self.pil.thumbnail((width,height), mode)
self.width, self.height = self.pil.size
return self
def compositeWith(self, iimg, mask):
self.pil = Image.composite(self.pil, iimg.pil, mask.pil.convert('L'))
return self
def fillPolygon(self, points, color):
maskdraw = ImageDraw.Draw(self.pil)
maskdraw.polygon(points, color)
return self
def copy(self):
return IImage(self.pil.copy())
def makeSquare(self):
side = min(self.width, self.height)
box = (
(self.width - side) // 2,
(self.height - side) // 2,
(self.width + side) // 2,
(self.height + side) // 2
)
self.pil = self.pil.crop(box)
self.width, self.height = self.pil.size
return self
def mirrorTopLeft(self):
points = [
(0, 0),
(0, self.height),
(self.width, 0)
]
mask = IImage((self.width, self.height))
mask.fillPolygon(points, "white")
rotated = self.copy().rotate(-90).flipV()
self.compositeWith(rotated, mask)
return self
def mirrorTopRight(self):
points = [
(0, 0),
(self.width, 0),
(self.width, self.height)
]
mask = IImage((self.width, self.height))
mask.fillPolygon(points, "white")
rotated = self.copy().rotate(90).flipV()
self.compositeWith(rotated, mask)
return self
def mirrorBottomLeft(self):
points = [
(0, 0),
(0, self.height),
(self.width, self.height)
]
mask = IImage((self.width, self.height))
mask.fillPolygon(points, "white")
rotated = self.copy().rotate(90).flipV()
self.compositeWith(rotated, mask)
return self
def mirrorBottomRight(self):
points = [
(0, self.height),
(self.width, self.height),
(self.width, 0)
]
mask = IImage((self.width, self.height))
mask.fillPolygon(points, "white")
rotated = self.copy().rotate(-90).flipV()
self.compositeWith(rotated, mask)
return self
def blendWith(self, iimg, alpha=0.5):
self.pil = Image.blend(self.pil, iimg.pil, alpha)
return self
def kaleidoscope(self, keepOriginalSize=True):
img0 = self.copy().flipH()
img1 = self.copy().flipV()
img2 = self.copy().flipHV()
return IImage.combine(self, img0, img1, img2, keepOriginalSize)
@classmethod
def combine(cls, iimg0, iimg1, iimg2, iimg3, keepOriginalSize=True):
width, height = iimg0.width, iimg0.height
newSize = (width*2, height*2)
canvas = IImage(newSize)
canvas.paste(iimg0, 0, 0)
canvas.paste(iimg1, width, 0)
canvas.paste(iimg2, 0, height)
canvas.paste(iimg3, width, height)
if keepOriginalSize:
return canvas.thumbnail(width, height, Image.NEAREST)
return canvas