author | Tero Marttila <terom@fixme.fi> |
Tue, 05 May 2009 18:08:48 +0300 | |
changeset 9 | efb80785ca9b |
parent 8 | 44d5ead35f4b |
child 10 | 6add80d3993b |
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 |
5
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
8 |
import random, itertools |
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 |
||
9 | 98 |
def build_data (text, chars, line_colors, random_chars, random_text) : |
0 | 99 |
""" |
100 |
Returns a matrix of (text, color) tuples representing the data to render |
|
101 |
||
102 |
[ [ (str, str) ] ] |
|
103 |
||
104 |
text - list of lines |
|
105 |
chars - list of random chars to interpse |
|
106 |
line_colors - list of colors to draw the chars in |
|
9 | 107 |
random_chars - randomize the lines the chars go in |
108 |
random_text - randomize the chars in each line |
|
0 | 109 |
""" |
110 |
||
111 |
data = [] |
|
9 | 112 |
|
113 |
if random_chars : |
|
114 |
chars = randomize(chars) |
|
0 | 115 |
|
5
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
116 |
for line, char, color in itertools.izip_longest(text, chars, line_colors, fillvalue=None) : |
0 | 117 |
# pick position to place char |
118 |
pos = random.randint(1, len(line) - 1) |
|
5
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
119 |
|
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
120 |
if not color : |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
121 |
color = "#000000" |
9 | 122 |
|
123 |
if random_text : |
|
124 |
line = ''.join(randomize(line)) |
|
0 | 125 |
|
126 |
# split into three parts |
|
5
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
127 |
if char : |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
128 |
data.append([ |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
129 |
(line[:pos], "#000000"), |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
130 |
(char, color), |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
131 |
(line[pos:], "#000000"), |
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 |
else : |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
134 |
data.append([ |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
135 |
(line, "#000000"), |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
136 |
]) |
0 | 137 |
|
138 |
return data |
|
139 |
||
140 |
def load_font (font_name, font_size) : |
|
141 |
""" |
|
142 |
Load a font by name |
|
143 |
""" |
|
144 |
||
145 |
# load font |
|
8 | 146 |
font_path = FONTS[font_name] |
0 | 147 |
font = ImageFont.truetype(font_path, font_size) |
148 |
||
149 |
return font |
|
150 |
||
151 |
def render_img (data, font, background_color="#ffffff", line_spacing=0) : |
|
152 |
""" |
|
153 |
Render the data (as from build_data) as an image, using the given PIL.ImageFont, and return the PIL Image object |
|
154 |
""" |
|
155 |
||
156 |
img_width = img_height = 0 |
|
157 |
||
158 |
img_data = [] |
|
159 |
||
160 |
# compute image width/height |
|
161 |
for segments in data : |
|
162 |
line_width = line_height = 0 |
|
163 |
||
164 |
# build a new list of segments with additional info |
|
165 |
line_segments = [] |
|
166 |
||
167 |
for seg_text, seg_color in segments : |
|
168 |
# compute rendered text size |
|
169 |
seg_width, seg_height = font.getsize(seg_text) |
|
170 |
||
171 |
# update line_* |
|
172 |
line_width += seg_width |
|
173 |
line_height = max(line_height, seg_height) |
|
174 |
||
175 |
# build the new segments list |
|
176 |
line_segments.append((seg_text, seg_color, seg_width)) |
|
177 |
||
178 |
# update img_* |
|
179 |
img_width = max(img_width, line_width) |
|
180 |
img_height += line_height |
|
181 |
img_data.append((line_segments, line_height)) |
|
182 |
||
183 |
# calculate height needed for line spacing |
|
184 |
img_height += (len(img_data) - 1) * line_spacing |
|
185 |
||
186 |
# create image |
|
187 |
img = Image.new("RGB", (img_width, img_height), background_color) |
|
188 |
draw = ImageDraw.Draw(img) |
|
189 |
||
190 |
# draw text |
|
191 |
img_y = 0 |
|
192 |
for segments, line_height in img_data : |
|
193 |
img_x = 0 |
|
194 |
||
195 |
# draw each segment build above, incremeing along img_x |
|
196 |
for seg_text, seg_color, seg_width in segments : |
|
197 |
draw.text((img_x, img_y), seg_text, font=font, fill=seg_color) |
|
198 |
||
199 |
img_x += seg_width |
|
200 |
||
201 |
img_y += line_height + line_spacing |
|
202 |
||
203 |
return img |
|
204 |
||
3 | 205 |
def effect_sharpness (img, factor) : |
1
71c7382994c4
rename logo.py -> index.cgi, and implement blurring
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
206 |
""" |
3 | 207 |
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
|
208 |
""" |
71c7382994c4
rename logo.py -> index.cgi, and implement blurring
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
209 |
|
71c7382994c4
rename logo.py -> index.cgi, and implement blurring
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
210 |
return ImageEnhance.Sharpness(img).enhance(factor) |
71c7382994c4
rename logo.py -> index.cgi, and implement blurring
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
211 |
|
7 | 212 |
def build_img (img, format='png') : |
0 | 213 |
""" |
214 |
Write the given PIL.Image as a string, returning the raw binary data |
|
7 | 215 |
|
216 |
Format should be one of the PIL-supported image foarts |
|
0 | 217 |
""" |
218 |
||
219 |
# render PNG output |
|
220 |
buf = StringIO() |
|
7 | 221 |
img.save(buf, format) |
0 | 222 |
data = buf.getvalue() |
223 |
||
224 |
return data |
|
225 |
||
3 | 226 |
def arg_bool (val) : |
227 |
if val.lower() in ('true', 't', '1', 'yes', 'y') : |
|
228 |
return True |
|
229 |
elif val.lower() in ('false', 'f', '0', 'no', 'n') : |
|
230 |
return False |
|
231 |
else : |
|
232 |
raise ValueError(val) |
|
233 |
||
234 |
def arg_color (val) : |
|
235 |
if val.beginswith('#') : |
|
236 |
int(val[1:], 16) |
|
237 |
||
238 |
return val |
|
239 |
else : |
|
240 |
raise ValueError(val) |
|
241 |
||
8 | 242 |
class Option (object) : |
243 |
def __init__ (self, name, is_list, type, default, range) : |
|
244 |
self.name = name |
|
245 |
self.is_list = is_list |
|
246 |
self.type = type |
|
247 |
self.default = default |
|
248 |
self.range = range |
|
3 | 249 |
|
8 | 250 |
def parse (self, args) : |
251 |
if self.is_list : |
|
252 |
if self.name in args : |
|
253 |
return args.getlist(self.name, self.type) |
|
254 |
else : |
|
255 |
return self.default |
|
256 |
else : |
|
257 |
return args.get(self.name, self.default, self.type) |
|
3 | 258 |
|
8 | 259 |
class Options (object) : |
260 |
def __init__ (self, *options) : |
|
261 |
self.options = options |
|
6 | 262 |
|
8 | 263 |
def parse (self, args) : |
264 |
return dict((opt.name, opt.parse(args)) for opt in self.options) |
|
265 |
||
266 |
OPTIONS = Options( |
|
267 |
Option('lang', False, str, Defaults.text_lang, TEXT_BY_LANG.keys()), |
|
268 |
Option('text', True, unicode, None, None), |
|
9 | 269 |
Option('random-text', False, arg_bool, False, None), |
8 | 270 |
Option('chars', True, unicode, Defaults.chars, None), |
271 |
Option('random-chars', False, arg_bool, True, None), |
|
272 |
Option('colors', True, arg_color, Defaults.colors, None), |
|
273 |
Option('font', False, str, Defaults.font_name, FONTS.keys()), |
|
274 |
Option('font-size', False, int, Defaults.font_size, None), |
|
275 |
Option('bg-color', False, arg_color, Defaults.bg_color, None), |
|
276 |
Option('line-spacing', False, int, Defaults.line_spacing, None), |
|
277 |
Option('sharpness', False, float, Defaults.sharpness, None), |
|
278 |
Option('image-format', False, str, Defaults.img_format, IMAGE_FORMATS.keys()), |
|
279 |
) |
|
280 |
||
281 |
def handle_help (req) : |
|
282 |
return werkzeug.Response('\n'.join( |
|
283 |
"%-15s %4s %-10s %-20s %s" % data for data in [ |
|
284 |
("name", "", "type", "default", "range"), |
|
285 |
("", "", "", "", ""), |
|
286 |
] + [( |
|
287 |
opt.name, |
|
288 |
'list' if opt.is_list else 'item', |
|
289 |
opt.type.__name__, |
|
290 |
repr(opt.default), |
|
291 |
opt.range if opt.range else "" |
|
292 |
) for opt in OPTIONS.options] |
|
293 |
), mimetype='text/plain') |
|
294 |
||
295 |
def handle_request (req) : |
|
296 |
if 'help' in req.args : |
|
297 |
return handle_help(req) |
|
298 |
||
299 |
# parse options |
|
300 |
opts = OPTIONS.parse(req.args) |
|
301 |
||
302 |
# postprocess |
|
303 |
if opts['text'] is None : |
|
304 |
opts['text'] = TEXT_BY_LANG[opts['lang']] |
|
305 |
||
306 |
if opts['font-size'] > FONT_SIZE_MAX : |
|
307 |
raise ValueError(opts['font-size']) |
|
3 | 308 |
|
309 |
# load/prep resources |
|
9 | 310 |
data = build_data(opts['text'], opts['chars'], opts['colors'], opts['random-chars'], opts['random-text']) |
8 | 311 |
font = load_font(opts['font'], opts['font-size']) |
3 | 312 |
|
313 |
# render the image |
|
8 | 314 |
img = render_img(data, font, opts['bg-color'], opts['line-spacing']) |
3 | 315 |
|
8 | 316 |
img = effect_sharpness(img, opts['sharpness']) |
3 | 317 |
|
8 | 318 |
png_data = build_img(img, opts['image-format']) |
3 | 319 |
|
320 |
# build the response |
|
8 | 321 |
response = werkzeug.Response(png_data, mimetype='image/%s' % opts['image-format']) |
3 | 322 |
|
323 |
return response |
|
324 |
||
325 |
@werkzeug.Request.application |
|
326 |
def wsgi_application (request) : |
|
0 | 327 |
""" |
3 | 328 |
Our werkzeug WSGI handler |
0 | 329 |
""" |
330 |
||
3 | 331 |
try : |
332 |
# request -> response |
|
333 |
response = handle_request(request) |
|
0 | 334 |
|
3 | 335 |
return response |
336 |
||
337 |
except HTTPException, e : |
|
338 |
# return as HTTP response |
|
339 |
return e |
|
0 | 340 |
|
341 |
def main () : |
|
3 | 342 |
handler = wsgiref.handlers.CGIHandler() |
343 |
app = wsgi_application |
|
344 |
||
6 | 345 |
if DEBUG : |
3 | 346 |
# enable debugging |
347 |
app = werkzeug.DebuggedApplication(app, evalex=False) |
|
0 | 348 |
|
3 | 349 |
handler.run(app) |
0 | 350 |
|
351 |
if __name__ == '__main__' : |
|
352 |
main() |
|
353 |