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 from PIL import ImageDraw;
40 from PIL import Image;
41
42
43
44
45
46
47
48
49
50
52
53
54
55
57 self.width = 0;
58 self.height = 0;
59 self.image = None;
60 self.draw = None;
61
62
63
64
65
67 self.image = Image.new('RGBA', (width, height), (0, 0, 0, 0));
68 self.width = width;
69 self.height = height;
70
71
72
73
74
75
76 - def setImage(self, image, width, height):
77 self.image = image;
78 self.width = width;
79 self.height = height;
80
81
82
83
85 if(self.draw == None):
86 self.draw = ImageDraw.Draw(self.image);
87 for element in elementList:
88 element.acceptVisitor(self);
89 return self.image;
90
91
92
93
94
96 return shapeSettings[1][0];
97
98
99
100
101
103 return shapeSettings[0][0];
104
105
106
107
108
110 return shapeSettings[0][1];
111
112
113
114
115
116
117
118
119
120 - def drawEllipse(self, cx, cy, rx, ry, shapeSettings, affineTransform = None):
121 x = cx-rx;
122 y = cy-ry;
123 w = x+rx*2;
124 h = y+ry*2;
125 fillColour = self.getFillColour(shapeSettings);
126 strokeColour = self.getStrokeColour(shapeSettings);
127 strokeWidth = self.getStrokeWidth(shapeSettings);
128 self.draw.ellipse((x,y,w,h), fill = fillColour, outline = strokeColour);
129
130
131
132
133
134
135
136
137
138 - def drawRectangle(self, x, y, w, h, shapeSettings, affineTransform = None):
139 fillColour = self.getFillColour(shapeSettings);
140 strokeColour = self.getStrokeColour(shapeSettings);
141 strokeWidth = self.getStrokeWidth(shapeSettings);
142 if(affineTransform==None):
143 self.draw.rectangle((x,y,w,h), fill = fillColour, outline = strokeColour);
144 else:
145 im = Image.new('RGBA', (self.width, self.height), (0, 0, 0, 0));
146 newDraw = ImageDraw.Draw(im);
147 newDraw.rectangle((x,y,w,h), fill = fillColour, outline = strokeColour);
148 newImage = im.transform((self.width,self.height), Image.AFFINE, affineTransform);
149 self.image.paste(newImage);
150
151
152
153
154
155
156
157 - def drawPolygon(self, pointTupleList, shapeSettings, affineTransform = None):
158 fillColour = self.getFillColour(shapeSettings);
159 strokeColour = self.getStrokeColour(shapeSettings);
160 strokeWidth = self.getStrokeWidth(shapeSettings);
161 if(affineTransform==None):
162 self.draw.polygon(pointTupleList, fill = fillColour, outline = strokeColour);
163 else:
164 im = Image.new('RGBA', (self.width, self.height), (0, 0, 0, 0));
165 newDraw = ImageDraw.Draw(im);
166 self.draw.polygon(pointTupleList, fill = fillColour, outline = strokeColour);
167 newImage = im.transform((self.width,self.height), Image.AFFINE, affineTransform);
168 self.image.paste(newImage);
169
170
171
172
173
174
175
176
177
178 - def drawLine(self, x1, y1, x2, y2, shapeSettings, affineTransform = None):
179 fillColour = self.getFillColour(shapeSettings);
180 strokeColour = self.getStrokeColour(shapeSettings);
181 strokeWidth = self.getStrokeWidth(shapeSettings);
182 if(affineTransform==None):
183 self.draw.line([(x1, y1), (x2, y2)], fill = strokeColour, width = strokeWidth);
184 else:
185 im = Image.new('RGBA', (self.width, self.height), (0, 0, 0, 0));
186 newDraw = ImageDraw.Draw(im);
187 self.draw.line([(x1, y1), (x2, y2)], fill = strokeColour, width = strokeWidth);
188 newImage = im.transform((self.width,self.height), Image.AFFINE, affineTransform);
189 self.image.paste(newImage);
190
191
192
193
194
195
196 - def drawPolyline(self, pointTupleList, shapeSettings, affineTransform = None):
197 fillColour = self.getFillColour(shapeSettings);
198 strokeColour = self.getStrokeColour(shapeSettings);
199 strokeWidth = self.getStrokeWidth(shapeSettings);
200 if(affineTransform==None):
201 self.draw.line(pointTupleList, fill = fillColour, width = strokeWidth);
202 else:
203 im = Image.new('RGBA', (self.width, self.height), (0, 0, 0, 0));
204 newDraw = ImageDraw.Draw(im);
205 self.draw.line(pointTupleList, fill = strokeColour, width = strokeColour);
206 newImage = im.transform((self.width,self.height), Image.AFFINE, affineTransform);
207 self.image.paste(newImage);
208
209
210
211
212
213
214
215
216
217
218 - def drawMask(self, x, y, width, height, bytes, shapeSettings, affineTransform = None):
219 fillColour = self.getFillColour(shapeSettings);
220 mask = Image.fromstring('1', (width, height), bytes);
221 if(affineTransform==None):
222 self.draw.bitmap(x, y, mask, fill = fillColour);
223 else:
224 im = Image.new('RGBA', (self.width, self.height), (0, 0, 0, 0));
225 newDraw = ImageDraw.Draw(im);
226 self.draw.bitmap(x, y, mask, fill = fillColour);
227 newImage = im.transform((self.width,self.height), Image.AFFINE, affineTransform);
228 self.image.paste(newImage);
229
230
231
232
233
234
235
236
237 - def drawText(self, x, y, text, shapeSettings, affineTransform = None):
238 textColour = self.getStrokeColour(shapeSettings);
239 if(affineTransform==None):
240 self.draw.text((x, y), text, fill = textColour);
241 else:
242 im = Image.new('RGBA', (self.width, self.height), (0, 0, 0, 0));
243 newDraw = ImageDraw.Draw(im);
244 self.draw.text((x, y), text, fill = textColour);
245 newImage = im.transform((self.width,self.height), Image.AFFINE, affineTransform);
246 self.image.paste(newImage);
247