1 #!/usr/bin/python2.5 |
|
2 import werkzeug |
|
3 from werkzeug.exceptions import HTTPException |
|
4 import wsgiref.handlers |
|
5 |
|
6 from PIL import Image, ImageDraw, ImageFont, ImageEnhance |
|
7 from cStringIO import StringIO |
|
8 import random, itertools, time |
|
9 |
|
10 if not hasattr(itertools, 'izip_longest') : |
|
11 |
|
12 def izip_longest(*args, **kwds): |
|
13 # izip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D- |
|
14 fillvalue = kwds.get('fillvalue') |
|
15 def sentinel(counter = ([fillvalue]*(len(args)-1)).pop): |
|
16 yield counter() # yields the fillvalue, or raises IndexError |
|
17 fillers = itertools.repeat(fillvalue) |
|
18 iters = [itertools.chain(it, sentinel(), fillers) for it in args] |
|
19 try: |
|
20 for tup in itertools.izip(*iters): |
|
21 yield tup |
|
22 except IndexError: |
|
23 pass |
|
24 |
|
25 itertools.izip_longest = izip_longest |
|
26 |
|
27 class Defaults : |
|
28 # settings |
|
29 |
|
30 text_lang = 'en' |
|
31 |
|
32 chars = [ u'"', u'!', u'?' ] |
|
33 |
|
34 colors = [ |
|
35 "#0469af", |
|
36 "#fbc614", |
|
37 "#e1313b", |
|
38 ] |
|
39 |
|
40 font_name = 'helvetica' |
|
41 font_size = 30 |
|
42 |
|
43 bg_color = "#ffffff" |
|
44 line_spacing = -10 |
|
45 sharpness = 0.6 |
|
46 |
|
47 img_format = 'png' |
|
48 |
|
49 TEXT_BY_LANG = dict( |
|
50 en = [ |
|
51 u"aalto", |
|
52 u"unive", |
|
53 u"rsity" |
|
54 ], |
|
55 fi = [ |
|
56 u"aalto", |
|
57 u"yliop", |
|
58 u"isto" |
|
59 ], |
|
60 se = [ |
|
61 u"aalto", |
|
62 u"univer", |
|
63 u"sitetet", |
|
64 ], |
|
65 ) |
|
66 |
|
67 FONTS = { |
|
68 'dejavu-sans-bold': "/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans-Bold.ttf", |
|
69 'helvetica': "HELR65W.TTF", |
|
70 } |
|
71 |
|
72 IMAGE_FORMATS = { |
|
73 'jpeg': 'jpeg', |
|
74 'jpg': 'jpeg', |
|
75 'png': 'png', |
|
76 'bmp': 'bmp' |
|
77 } |
|
78 |
|
79 FONT_SIZE_MAX = 1024 |
|
80 |
|
81 # enable debugging |
|
82 DEBUG = True |
|
83 |
|
84 |
|
85 def randomize (seq) : |
|
86 """ |
|
87 Returns the given sequence in random order as a list |
|
88 """ |
|
89 |
|
90 # copy |
|
91 l = list(seq) |
|
92 |
|
93 # rearrange |
|
94 random.shuffle(l) |
|
95 |
|
96 return l |
|
97 |
|
98 def randomize_str_char (str) : |
|
99 """ |
|
100 Randomize the given string by moving one char around |
|
101 """ |
|
102 |
|
103 l = list(str) |
|
104 |
|
105 c = l.pop(random.randint(0, len(l) - 1)) |
|
106 l.insert(random.randint(0, len(l)), c) |
|
107 |
|
108 return ''.join(l) |
|
109 |
|
110 def build_data (text, chars, line_colors, random_chars=True, random_text=False, random_text_char=False) : |
|
111 """ |
|
112 Returns a matrix of (text, color) tuples representing the data to render |
|
113 |
|
114 [ [ (str, str) ] ] |
|
115 |
|
116 text - list of lines |
|
117 chars - list of random chars to interpse |
|
118 line_colors - list of colors to draw the chars in |
|
119 random_chars - randomize the lines the chars go in |
|
120 random_text - randomize the chars in each line |
|
121 random_text_char - randomize each line by moving one char around |
|
122 """ |
|
123 |
|
124 data = [] |
|
125 |
|
126 if random_chars : |
|
127 chars = randomize(chars) |
|
128 |
|
129 for line, char, color in itertools.izip_longest(text, chars, line_colors, fillvalue=None) : |
|
130 # pick position to place char |
|
131 pos = random.randint(1, len(line) - 1) |
|
132 |
|
133 if not color : |
|
134 color = "#000000" |
|
135 |
|
136 if random_text : |
|
137 line = ''.join(randomize(line)) |
|
138 |
|
139 if random_text_char : |
|
140 line = randomize_str_char(line) |
|
141 |
|
142 # split into three parts |
|
143 if char : |
|
144 data.append([ |
|
145 (line[:pos], "#000000"), |
|
146 (char, color), |
|
147 (line[pos:], "#000000"), |
|
148 ]) |
|
149 else : |
|
150 data.append([ |
|
151 (line, "#000000"), |
|
152 ]) |
|
153 |
|
154 return data |
|
155 |
|
156 def load_font (font_name, font_size) : |
|
157 """ |
|
158 Load a font by name |
|
159 """ |
|
160 |
|
161 # load font |
|
162 font_path = FONTS[font_name] |
|
163 font = ImageFont.truetype(font_path, font_size) |
|
164 |
|
165 return font |
|
166 |
|
167 def render_img (data, font, background_color="#ffffff", line_spacing=0) : |
|
168 """ |
|
169 Render the data (as from build_data) as an image, using the given PIL.ImageFont, and return the PIL Image object |
|
170 """ |
|
171 |
|
172 img_width = img_height = 0 |
|
173 |
|
174 img_data = [] |
|
175 |
|
176 # compute image width/height |
|
177 for segments in data : |
|
178 line_width = line_height = 0 |
|
179 |
|
180 # build a new list of segments with additional info |
|
181 line_segments = [] |
|
182 |
|
183 for seg_text, seg_color in segments : |
|
184 # compute rendered text size |
|
185 seg_width, seg_height = font.getsize(seg_text) |
|
186 |
|
187 # update line_* |
|
188 line_width += seg_width |
|
189 line_height = max(line_height, seg_height) |
|
190 |
|
191 # build the new segments list |
|
192 line_segments.append((seg_text, seg_color, seg_width)) |
|
193 |
|
194 # update img_* |
|
195 img_width = max(img_width, line_width) |
|
196 img_height += line_height |
|
197 img_data.append((line_segments, line_height)) |
|
198 |
|
199 # calculate height needed for line spacing |
|
200 img_height += (len(img_data) - 1) * line_spacing |
|
201 |
|
202 # create image |
|
203 img = Image.new("RGB", (img_width, img_height), background_color) |
|
204 draw = ImageDraw.Draw(img) |
|
205 |
|
206 # draw text |
|
207 img_y = 0 |
|
208 for segments, line_height in img_data : |
|
209 img_x = 0 |
|
210 |
|
211 # draw each segment build above, incremeing along img_x |
|
212 for seg_text, seg_color, seg_width in segments : |
|
213 draw.text((img_x, img_y), seg_text, font=font, fill=seg_color) |
|
214 |
|
215 img_x += seg_width |
|
216 |
|
217 img_y += line_height + line_spacing |
|
218 |
|
219 return img |
|
220 |
|
221 def effect_sharpness (img, factor) : |
|
222 """ |
|
223 Sharpen the image by the given factor |
|
224 """ |
|
225 |
|
226 return ImageEnhance.Sharpness(img).enhance(factor) |
|
227 |
|
228 def build_img (img, format='png') : |
|
229 """ |
|
230 Write the given PIL.Image as a string, returning the raw binary data |
|
231 |
|
232 Format should be one of the PIL-supported image foarts |
|
233 """ |
|
234 |
|
235 # render PNG output |
|
236 buf = StringIO() |
|
237 img.save(buf, format) |
|
238 data = buf.getvalue() |
|
239 |
|
240 return data |
|
241 |
|
242 def arg_bool (val) : |
|
243 if val.lower() in ('true', 't', '1', 'yes', 'y') : |
|
244 return True |
|
245 elif val.lower() in ('false', 'f', '0', 'no', 'n') : |
|
246 return False |
|
247 else : |
|
248 raise ValueError(val) |
|
249 |
|
250 def arg_color (val) : |
|
251 if val.beginswith('#') : |
|
252 int(val[1:], 16) |
|
253 |
|
254 return val |
|
255 else : |
|
256 raise ValueError(val) |
|
257 |
|
258 class Option (object) : |
|
259 def __init__ (self, name, is_list, type, default, range) : |
|
260 self.name = name |
|
261 self.is_list = is_list |
|
262 self.type = type |
|
263 self.default = default |
|
264 self.range = range |
|
265 |
|
266 def parse (self, args) : |
|
267 if self.is_list : |
|
268 if self.name in args : |
|
269 return args.getlist(self.name, self.type) |
|
270 else : |
|
271 return self.default |
|
272 else : |
|
273 if self.type == arg_bool and not self.default and self.name in args : |
|
274 return True |
|
275 |
|
276 else : |
|
277 return args.get(self.name, self.default, self.type) |
|
278 |
|
279 class Options (object) : |
|
280 def __init__ (self, *options) : |
|
281 self.options = options |
|
282 |
|
283 def parse (self, args) : |
|
284 return dict((opt.name, opt.parse(args)) for opt in self.options) |
|
285 |
|
286 OPTIONS = Options( |
|
287 Option('lang', False, str, Defaults.text_lang, TEXT_BY_LANG.keys()), |
|
288 Option('text', True, unicode, None, None), |
|
289 Option('random-text', False, arg_bool, False, None), |
|
290 Option('random-text-char', False, arg_bool, False, None), |
|
291 Option('chars', True, unicode, Defaults.chars, None), |
|
292 Option('random-chars', False, arg_bool, True, None), |
|
293 Option('colors', True, arg_color, Defaults.colors, None), |
|
294 Option('font', False, str, Defaults.font_name, FONTS.keys()), |
|
295 Option('font-size', False, int, Defaults.font_size, None), |
|
296 Option('bg-color', False, arg_color, Defaults.bg_color, None), |
|
297 Option('line-spacing', False, int, Defaults.line_spacing, None), |
|
298 Option('sharpness', False, float, Defaults.sharpness, None), |
|
299 Option('image-format', False, str, Defaults.img_format, IMAGE_FORMATS.keys()), |
|
300 Option('seed', False, int, None, None), |
|
301 ) |
|
302 |
|
303 def handle_help (req) : |
|
304 return werkzeug.Response('\n'.join( |
|
305 "%-15s %4s %-10s %-20s %s" % data for data in [ |
|
306 ("name", "", "type", "default", "range"), |
|
307 ("", "", "", "", ""), |
|
308 ] + [( |
|
309 opt.name, |
|
310 'list' if opt.is_list else 'item', |
|
311 opt.type.__name__, |
|
312 repr(opt.default), |
|
313 opt.range if opt.range else "" |
|
314 ) for opt in OPTIONS.options] |
|
315 ), mimetype='text/plain') |
|
316 |
|
317 def handle_request (req) : |
|
318 if 'help' in req.args : |
|
319 return handle_help(req) |
|
320 |
|
321 # parse options |
|
322 opts = OPTIONS.parse(req.args) |
|
323 |
|
324 # postprocess |
|
325 if opts['text'] is None : |
|
326 opts['text'] = TEXT_BY_LANG[opts['lang']] |
|
327 |
|
328 if opts['font-size'] > FONT_SIZE_MAX : |
|
329 raise ValueError(opts['font-size']) |
|
330 |
|
331 if opts['seed'] is None : |
|
332 opts['seed'] = time.time() |
|
333 |
|
334 # load/prep resources |
|
335 random.seed(opts['seed']) |
|
336 data = build_data(opts['text'], opts['chars'], opts['colors'], opts['random-chars'], opts['random-text'], opts['random-text-char']) |
|
337 font = load_font(opts['font'], opts['font-size']) |
|
338 |
|
339 # render the image |
|
340 img = render_img(data, font, opts['bg-color'], opts['line-spacing']) |
|
341 |
|
342 img = effect_sharpness(img, opts['sharpness']) |
|
343 |
|
344 png_data = build_img(img, opts['image-format']) |
|
345 |
|
346 # build the response |
|
347 response = werkzeug.Response(png_data, mimetype='image/%s' % opts['image-format']) |
|
348 |
|
349 return response |
|
350 |
|
351 @werkzeug.Request.application |
|
352 def wsgi_application (request) : |
|
353 """ |
|
354 Our werkzeug WSGI handler |
|
355 """ |
|
356 |
|
357 try : |
|
358 # request -> response |
|
359 response = handle_request(request) |
|
360 |
|
361 return response |
|
362 |
|
363 except HTTPException, e : |
|
364 # return as HTTP response |
|
365 return e |
|
366 |
|
367 def main () : |
|
368 handler = wsgiref.handlers.CGIHandler() |
|
369 app = wsgi_application |
|
370 |
|
371 if DEBUG : |
|
372 # enable debugging |
|
373 app = werkzeug.DebuggedApplication(app, evalex=False) |
|
374 |
|
375 handler.run(app) |
|
376 |
|
377 if __name__ == '__main__' : |
|
378 main() |
|
379 |
|