author | Tero Marttila <terom@fixme.fi> |
Tue, 05 May 2009 18:25:16 +0300 | |
changeset 12 | aa6b83c94528 |
parent 11 | 0f070e9daa71 |
permissions | -rwxr-xr-x |
0 | 1 |
#!/usr/bin/python2.5 |
3 | 2 |
import werkzeug |
3 |
from werkzeug.exceptions import HTTPException |
|
4 |
import wsgiref.handlers |
|
0 | 5 |
|
1
71c7382994c4
rename logo.py -> index.cgi, and implement blurring
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
6 |
from PIL import Image, ImageDraw, ImageFont, ImageEnhance |
0 | 7 |
from cStringIO import StringIO |
12 | 8 |
import random, itertools, time |
0 | 9 |
|
5
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
10 |
if not hasattr(itertools, 'izip_longest') : |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
11 |
|
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
12 |
def izip_longest(*args, **kwds): |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
13 |
# izip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D- |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
14 |
fillvalue = kwds.get('fillvalue') |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
15 |
def sentinel(counter = ([fillvalue]*(len(args)-1)).pop): |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
16 |
yield counter() # yields the fillvalue, or raises IndexError |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
17 |
fillers = itertools.repeat(fillvalue) |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
18 |
iters = [itertools.chain(it, sentinel(), fillers) for it in args] |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
19 |
try: |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
20 |
for tup in itertools.izip(*iters): |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
21 |
yield tup |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
22 |
except IndexError: |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
23 |
pass |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
24 |
|
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
25 |
itertools.izip_longest = izip_longest |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
26 |
|
3 | 27 |
class Defaults : |
28 |
# settings |
|
8 | 29 |
|
30 |
text_lang = 'en' |
|
0 | 31 |
|
7 | 32 |
chars = [ u'"', u'!', u'?' ] |
0 | 33 |
|
8 | 34 |
colors = [ |
3 | 35 |
"#0469af", |
36 |
"#fbc614", |
|
37 |
"#e1313b", |
|
38 |
] |
|
39 |
||
40 |
font_name = 'helvetica' |
|
41 |
font_size = 30 |
|
42 |
||
8 | 43 |
bg_color = "#ffffff" |
3 | 44 |
line_spacing = -10 |
45 |
sharpness = 0.6 |
|
0 | 46 |
|
7 | 47 |
img_format = 'png' |
48 |
||
8 | 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 = { |
|
3 | 68 |
'dejavu-sans-bold': "/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans-Bold.ttf", |
69 |
'helvetica': "HELR65W.TTF", |
|
70 |
} |
|
0 | 71 |
|
8 | 72 |
IMAGE_FORMATS = { |
73 |
'jpeg': 'jpeg', |
|
74 |
'jpg': 'jpeg', |
|
75 |
'png': 'png', |
|
76 |
'bmp': 'bmp' |
|
77 |
} |
|
78 |
||
6 | 79 |
FONT_SIZE_MAX = 1024 |
3 | 80 |
|
81 |
# enable debugging |
|
6 | 82 |
DEBUG = True |
83 |
||
0 | 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 |
||
11 | 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) : |
|
0 | 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 |
|
11 | 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 |
|
0 | 122 |
""" |
123 |
||
124 |
data = [] |
|
9 | 125 |
|
126 |
if random_chars : |
|
127 |
chars = randomize(chars) |
|
0 | 128 |
|
5
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
129 |
for line, char, color in itertools.izip_longest(text, chars, line_colors, fillvalue=None) : |
0 | 130 |
# pick position to place char |
131 |
pos = random.randint(1, len(line) - 1) |
|
5
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
132 |
|
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
133 |
if not color : |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
134 |
color = "#000000" |
9 | 135 |
|
136 |
if random_text : |
|
137 |
line = ''.join(randomize(line)) |
|
11 | 138 |
|
139 |
if random_text_char : |
|
140 |
line = randomize_str_char(line) |
|
0 | 141 |
|
142 |
# split into three parts |
|
5
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
143 |
if char : |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
144 |
data.append([ |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
145 |
(line[:pos], "#000000"), |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
146 |
(char, color), |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
147 |
(line[pos:], "#000000"), |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
148 |
]) |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
149 |
else : |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
150 |
data.append([ |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
151 |
(line, "#000000"), |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
152 |
]) |
0 | 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 |
|
8 | 162 |
font_path = FONTS[font_name] |
0 | 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 |
||
3 | 221 |
def effect_sharpness (img, factor) : |
1
71c7382994c4
rename logo.py -> index.cgi, and implement blurring
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
222 |
""" |
3 | 223 |
Sharpen the image by the given factor |
1
71c7382994c4
rename logo.py -> index.cgi, and implement blurring
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
224 |
""" |
71c7382994c4
rename logo.py -> index.cgi, and implement blurring
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
225 |
|
71c7382994c4
rename logo.py -> index.cgi, and implement blurring
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
226 |
return ImageEnhance.Sharpness(img).enhance(factor) |
71c7382994c4
rename logo.py -> index.cgi, and implement blurring
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
227 |
|
7 | 228 |
def build_img (img, format='png') : |
0 | 229 |
""" |
230 |
Write the given PIL.Image as a string, returning the raw binary data |
|
7 | 231 |
|
232 |
Format should be one of the PIL-supported image foarts |
|
0 | 233 |
""" |
234 |
||
235 |
# render PNG output |
|
236 |
buf = StringIO() |
|
7 | 237 |
img.save(buf, format) |
0 | 238 |
data = buf.getvalue() |
239 |
||
240 |
return data |
|
241 |
||
3 | 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 |
||
8 | 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 |
|
3 | 265 |
|
8 | 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 : |
|
10
6add80d3993b
enable arg_bools by just giving an empty opt
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
273 |
if self.type == arg_bool and not self.default and self.name in args : |
6add80d3993b
enable arg_bools by just giving an empty opt
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
274 |
return True |
6add80d3993b
enable arg_bools by just giving an empty opt
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
275 |
|
6add80d3993b
enable arg_bools by just giving an empty opt
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
276 |
else : |
6add80d3993b
enable arg_bools by just giving an empty opt
Tero Marttila <terom@fixme.fi>
parents:
9
diff
changeset
|
277 |
return args.get(self.name, self.default, self.type) |
3 | 278 |
|
8 | 279 |
class Options (object) : |
280 |
def __init__ (self, *options) : |
|
281 |
self.options = options |
|
6 | 282 |
|
8 | 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), |
|
9 | 289 |
Option('random-text', False, arg_bool, False, None), |
11 | 290 |
Option('random-text-char', False, arg_bool, False, None), |
8 | 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()), |
|
12 | 300 |
Option('seed', False, int, None, None), |
8 | 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']) |
|
3 | 330 |
|
12 | 331 |
if opts['seed'] is None : |
332 |
opts['seed'] = time.time() |
|
333 |
||
3 | 334 |
# load/prep resources |
12 | 335 |
random.seed(opts['seed']) |
11 | 336 |
data = build_data(opts['text'], opts['chars'], opts['colors'], opts['random-chars'], opts['random-text'], opts['random-text-char']) |
8 | 337 |
font = load_font(opts['font'], opts['font-size']) |
3 | 338 |
|
339 |
# render the image |
|
8 | 340 |
img = render_img(data, font, opts['bg-color'], opts['line-spacing']) |
3 | 341 |
|
8 | 342 |
img = effect_sharpness(img, opts['sharpness']) |
3 | 343 |
|
8 | 344 |
png_data = build_img(img, opts['image-format']) |
3 | 345 |
|
346 |
# build the response |
|
8 | 347 |
response = werkzeug.Response(png_data, mimetype='image/%s' % opts['image-format']) |
3 | 348 |
|
349 |
return response |
|
350 |
||
351 |
@werkzeug.Request.application |
|
352 |
def wsgi_application (request) : |
|
0 | 353 |
""" |
3 | 354 |
Our werkzeug WSGI handler |
0 | 355 |
""" |
356 |
||
3 | 357 |
try : |
358 |
# request -> response |
|
359 |
response = handle_request(request) |
|
0 | 360 |
|
3 | 361 |
return response |
362 |
||
363 |
except HTTPException, e : |
|
364 |
# return as HTTP response |
|
365 |
return e |
|
0 | 366 |
|
367 |
def main () : |
|
3 | 368 |
handler = wsgiref.handlers.CGIHandler() |
369 |
app = wsgi_application |
|
370 |
||
6 | 371 |
if DEBUG : |
3 | 372 |
# enable debugging |
373 |
app = werkzeug.DebuggedApplication(app, evalex=False) |
|
0 | 374 |
|
3 | 375 |
handler.run(app) |
0 | 376 |
|
377 |
if __name__ == '__main__' : |
|
378 |
main() |
|
379 |