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