-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.py
204 lines (168 loc) · 6.64 KB
/
image.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
import math, zlib
import Image, ImageDraw, ImageChops
from utils import floodfill, bucketfill
class PyntImage(object):
"""Abstract class for images: only for subclassing"""
transp_color = 0
mask_palette = [255,255,255]*256
def draw_rect(self, color, bbox, width=1, fill=None):
#self.draw.rectangle(bbox, outline=color, fill=fill)
if fill:
self.draw.rectangle(bbox, fill=fill)
else:
self.draw_line(color, width, (bbox[0],bbox[1], bbox[2],bbox[1]))
self.draw_line(color, width, (bbox[2],bbox[1], bbox[2],bbox[3]))
self.draw_line(color, width, (bbox[2],bbox[3], bbox[0],bbox[3]))
self.draw_line(color, width, (bbox[0],bbox[3], bbox[0],bbox[1]))
def draw_ellipse(self, color, bbox, fill=None):
self.draw.ellipse(bbox, outline=color, fill=fill)
def draw_line(self, color, width, bbox):
print "draw_line"
x0, y0, x1, y1 = bbox
#if y1 < y0:
# x0, x1 = x1, x0
# y0, y1 = y1, y0
#bbox = (x0, y0, x1, y1)
if width == 1:
print "x0=%d, y0=%d, x1=%d, y1=%d"%(x0,y0,x1,y1)
if x0 == x1 and y0 == y1:
self.draw.point((x0, y0), fill=color)
else:
self.draw.line(bbox, fill=color)
else:
dx, dy = x1 - x0, y1 - y0
if dx == dy == 0:
xy = (x0 - width / 2, y0 - width / 2,
x1 + width / 2, y1 + width / 2)
self.draw.ellipse(xy, fill=color)
l = math.sqrt(dx**2 + dy**2)
if l > 0:
sx = .5 * ((dx > 0 and 2) - 1)
sy = .5 * ((dy > 0 and 2) - 1)
dyn, dxn = int(((dx / l) * width / 2 + sx)), \
int(((dy / l) * width / 2 + sy))
poly = ((x0 - dxn, y0 + dyn), (x0 + dxn, y0 - dyn),
(x1 + dxn, y1 - dyn), (x1 - dxn, y1 + dyn))
self.draw.polygon(poly, fill=color)
return poly
else:
return None
def floodfill(self, color, xy):
return floodfill(self.data, xy, color)
def crop(self, bbox):
tmp = self.data.crop(bbox)
tmp.load()
return tmp
def paste(self, source, bbox=(0,0), mask = None):
if mask is None:
self.data.paste(source, bbox)
else:
tmp = self.make_mask(mask)
self.data.paste(source, bbox, tmp)
def paint(self, color, bbox=(0,0), mask=None):
if mask is None:
self.data.paste(color, bbox)
else:
tmp = self.make_mask(mask)
self.data.paste(color, bbox, tmp)
def erase(self, bbox=(0,0), mask=None):
#print "erasing:", self.transp_color
if mask is None:
self.data.paste(self.transp_color, bbox)
else:
tmp = self.make_mask(mask)
self.data.paste(self.transp_color, bbox, tmp)
def getpixel(self, pos):
return self.data.getpixel(pos)
def getcolors(self):
return self.data.getcolors()
def getbbox(self):
return self.data.getbbox()
def make_mask(self, img):
tmp = img.copy()
tmp.putpalette(self.mask_palette)
return tmp.convert("L")
def flip(self, vertically=False):
print "Flipping..."
if vertically:
self.data = self.data.transpose(Image.FLIP_TOP_BOTTOM)
else:
self.data = self.data.transpose(Image.FLIP_LEFT_RIGHT)
def rotate(self, angle):
self.data = self.data.rotate(angle)
self.size = self.data.size
def tozstring(self):
return zlib.compress(self.data.tostring())
def combine(self, other):
self.paste(other.data, mask=self.make_mask(other.data))
class PyntBrush(PyntImage):
"""Container for an arbitrary brush"""
def __init__(self, size=(1,1), color=1, transp_color=0, data=None):
if data is None:
self.data = self.make_elliptic_brush(size, color, transp_color)
self.custom_brush=False
else:
self.data = data
self.custom_brush=True
self.size = self.data.size
self.set_transp_color(transp_color)
self.solid_color = False
def make_elliptic_brush(self, size, color, bgcolor):
brush=Image.new("P", (size[0]+1, size[1]+1), bgcolor)
brush_draw = ImageDraw.Draw(brush)
brush_draw.ellipse((0,0,size[0],size[1]), fill=color)
return brush
def set_transp_color(self, n):
self.mask_palette[self.transp_color*3] = 255
self.mask_palette[self.transp_color*3+1] = 255
self.mask_palette[self.transp_color*3+2] = 255
self.transp_color = n
self.mask_palette[n*3] = 0
self.mask_palette[n*3+1] = 0
self.mask_palette[n*3+2] = 0
def get_mask(self):
return self.make_mask(self.data)
class PyntImagePalette(PyntImage):
"""Palette based image"""
def __init__(self, resolution=(400,600), fillcolor=0, data=None):
self.resolution = resolution
if data is None:
self.data = Image.new("P", resolution, fillcolor)
self.palette = (255,255,255, 0,0,0, 255,0,0, 0,255,0,
0,0,255, 255,255,0, 0,255,255, 255,0,255) * 32
self.data.putpalette(self.palette)
else:
self.data = data
self.draw = ImageDraw.Draw(self.data)
self.set_transp_color(0)
def set_palette(self, colors):
#print "setting palette..."
self.palette = colors
if len(colors) == 256:
colors = reduce(lambda x, y: x+y, colors)
self.data.putpalette(colors)
#print colors[:10]
self.draw = ImageDraw.Draw(self.data)
def set_transp_color(self, n):
self.mask_palette[self.transp_color*3] = 255
self.mask_palette[self.transp_color*3+1] = 255
self.mask_palette[self.transp_color*3+2] = 255
self.transp_color = n
self.mask_palette[n*3] = 0
self.mask_palette[n*3+1] = 0
self.mask_palette[n*3+2] = 0
class PyntImageRGBA(PyntImage):
"""RGB+alpha image"""
def __init__(self, resolution=(640, 480), fillcolor=(255,255,255,255), data=None):
self.resolution = resolution
if data is None:
self.data = Image.new("RGBA", resolution, fillcolor)
else:
self.data = data
self.draw = ImageDraw.Draw(self.data)
self.transp_color = (0,0,0,0)
#self.data = numpy.zeros((self.resolution[0], self.resolution[1], 4), dtype=numpy.uint8)
#self.data =
#self.data[:, :, :] = (255, 255, 255, 255)
def set_transp_color(self, c):
self.transp_color = c