author | Tero Marttila <terom@fixme.fi> |
Tue, 05 May 2009 17:32:27 +0300 | |
changeset 7 | 9a6ac93e7446 |
parent 6 | f61000aa264b |
child 8 | 44d5ead35f4b |
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 |
|
29 |
text = [ |
|
7 | 30 |
u"aalto", |
31 |
u"unive", |
|
32 |
u"rsity" |
|
3 | 33 |
] |
0 | 34 |
|
7 | 35 |
chars = [ u'"', u'!', u'?' ] |
0 | 36 |
|
3 | 37 |
line_colors = [ |
38 |
"#0469af", |
|
39 |
"#fbc614", |
|
40 |
"#e1313b", |
|
41 |
] |
|
42 |
||
43 |
font_name = 'helvetica' |
|
44 |
font_size = 30 |
|
45 |
||
46 |
background_color = "#ffffff" |
|
47 |
line_spacing = -10 |
|
48 |
sharpness = 0.6 |
|
0 | 49 |
|
7 | 50 |
img_format = 'png' |
51 |
||
0 | 52 |
fonts = { |
3 | 53 |
'dejavu-sans-bold': "/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans-Bold.ttf", |
54 |
'helvetica': "HELR65W.TTF", |
|
55 |
} |
|
0 | 56 |
|
6 | 57 |
FONT_SIZE_MAX = 1024 |
3 | 58 |
|
59 |
# enable debugging |
|
6 | 60 |
DEBUG = True |
61 |
||
0 | 62 |
|
63 |
def randomize (seq) : |
|
64 |
""" |
|
65 |
Returns the given sequence in random order as a list |
|
66 |
""" |
|
67 |
||
68 |
# copy |
|
69 |
l = list(seq) |
|
70 |
||
71 |
# rearrange |
|
72 |
random.shuffle(l) |
|
73 |
||
74 |
return l |
|
75 |
||
76 |
def build_data (text, chars, line_colors) : |
|
77 |
""" |
|
78 |
Returns a matrix of (text, color) tuples representing the data to render |
|
79 |
||
80 |
[ [ (str, str) ] ] |
|
81 |
||
82 |
text - list of lines |
|
83 |
chars - list of random chars to interpse |
|
84 |
line_colors - list of colors to draw the chars in |
|
85 |
""" |
|
86 |
||
87 |
data = [] |
|
88 |
||
5
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
89 |
for line, char, color in itertools.izip_longest(text, chars, line_colors, fillvalue=None) : |
0 | 90 |
# pick position to place char |
91 |
pos = random.randint(1, len(line) - 1) |
|
5
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
92 |
|
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
93 |
if not color : |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
94 |
color = "#000000" |
0 | 95 |
|
96 |
# split into three parts |
|
5
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
97 |
if char : |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
98 |
data.append([ |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
99 |
(line[:pos], "#000000"), |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
100 |
(char, color), |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
101 |
(line[pos:], "#000000"), |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
102 |
]) |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
103 |
else : |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
104 |
data.append([ |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
105 |
(line, "#000000"), |
6d0e03f2fef4
replace zip with itertools.izip_longest
Tero Marttila <terom@fixme.fi>
parents:
4
diff
changeset
|
106 |
]) |
0 | 107 |
|
108 |
return data |
|
109 |
||
110 |
def load_font (font_name, font_size) : |
|
111 |
""" |
|
112 |
Load a font by name |
|
113 |
""" |
|
114 |
||
115 |
# load font |
|
116 |
font_path = fonts[font_name] |
|
117 |
font = ImageFont.truetype(font_path, font_size) |
|
118 |
||
119 |
return font |
|
120 |
||
121 |
def render_img (data, font, background_color="#ffffff", line_spacing=0) : |
|
122 |
""" |
|
123 |
Render the data (as from build_data) as an image, using the given PIL.ImageFont, and return the PIL Image object |
|
124 |
""" |
|
125 |
||
126 |
img_width = img_height = 0 |
|
127 |
||
128 |
img_data = [] |
|
129 |
||
130 |
# compute image width/height |
|
131 |
for segments in data : |
|
132 |
line_width = line_height = 0 |
|
133 |
||
134 |
# build a new list of segments with additional info |
|
135 |
line_segments = [] |
|
136 |
||
137 |
for seg_text, seg_color in segments : |
|
138 |
# compute rendered text size |
|
139 |
seg_width, seg_height = font.getsize(seg_text) |
|
140 |
||
141 |
# update line_* |
|
142 |
line_width += seg_width |
|
143 |
line_height = max(line_height, seg_height) |
|
144 |
||
145 |
# build the new segments list |
|
146 |
line_segments.append((seg_text, seg_color, seg_width)) |
|
147 |
||
148 |
# update img_* |
|
149 |
img_width = max(img_width, line_width) |
|
150 |
img_height += line_height |
|
151 |
img_data.append((line_segments, line_height)) |
|
152 |
||
153 |
# calculate height needed for line spacing |
|
154 |
img_height += (len(img_data) - 1) * line_spacing |
|
155 |
||
156 |
# create image |
|
157 |
img = Image.new("RGB", (img_width, img_height), background_color) |
|
158 |
draw = ImageDraw.Draw(img) |
|
159 |
||
160 |
# draw text |
|
161 |
img_y = 0 |
|
162 |
for segments, line_height in img_data : |
|
163 |
img_x = 0 |
|
164 |
||
165 |
# draw each segment build above, incremeing along img_x |
|
166 |
for seg_text, seg_color, seg_width in segments : |
|
167 |
draw.text((img_x, img_y), seg_text, font=font, fill=seg_color) |
|
168 |
||
169 |
img_x += seg_width |
|
170 |
||
171 |
img_y += line_height + line_spacing |
|
172 |
||
173 |
return img |
|
174 |
||
3 | 175 |
def effect_sharpness (img, factor) : |
1
71c7382994c4
rename logo.py -> index.cgi, and implement blurring
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
176 |
""" |
3 | 177 |
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
|
178 |
""" |
71c7382994c4
rename logo.py -> index.cgi, and implement blurring
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
179 |
|
71c7382994c4
rename logo.py -> index.cgi, and implement blurring
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
180 |
return ImageEnhance.Sharpness(img).enhance(factor) |
71c7382994c4
rename logo.py -> index.cgi, and implement blurring
Tero Marttila <terom@fixme.fi>
parents:
0
diff
changeset
|
181 |
|
7 | 182 |
def build_img (img, format='png') : |
0 | 183 |
""" |
184 |
Write the given PIL.Image as a string, returning the raw binary data |
|
7 | 185 |
|
186 |
Format should be one of the PIL-supported image foarts |
|
0 | 187 |
""" |
188 |
||
189 |
# render PNG output |
|
190 |
buf = StringIO() |
|
7 | 191 |
img.save(buf, format) |
0 | 192 |
data = buf.getvalue() |
193 |
||
194 |
return data |
|
195 |
||
3 | 196 |
def arg_bool (val) : |
197 |
if val.lower() in ('true', 't', '1', 'yes', 'y') : |
|
198 |
return True |
|
199 |
elif val.lower() in ('false', 'f', '0', 'no', 'n') : |
|
200 |
return False |
|
201 |
else : |
|
202 |
raise ValueError(val) |
|
203 |
||
204 |
def arg_color (val) : |
|
205 |
if val.beginswith('#') : |
|
206 |
int(val[1:], 16) |
|
207 |
||
208 |
return val |
|
209 |
else : |
|
210 |
raise ValueError(val) |
|
211 |
||
212 |
def handle_request (req) : |
|
213 |
# parse args |
|
214 |
if 'text' in req.args : |
|
215 |
text = req.args.getlist('text', unicode) |
|
216 |
else : |
|
217 |
text = Defaults.text |
|
218 |
||
219 |
if 'chars' in req.args : |
|
220 |
chars = req.args.getlist('chars', unicode) |
|
221 |
else : |
|
222 |
chars = Defaults.chars |
|
223 |
||
224 |
if 'colors' in req.args : |
|
225 |
colors = req.args.getlist('colors', arg_color) |
|
226 |
||
227 |
else : |
|
228 |
colors = Defaults.line_colors |
|
229 |
||
230 |
font_name = req.args.get('font', Defaults.font_name) |
|
231 |
font_size = req.args.get('font-size', Defaults.font_size, int) |
|
232 |
background_color = req.args.get('background-color', Defaults.background_color, arg_color) |
|
233 |
line_spacing = req.args.get('line-spacing', Defaults.line_spacing, int) |
|
234 |
sharpness = req.args.get('sharpness', Defaults.sharpness, float) |
|
7 | 235 |
img_format = req.args.get('image-format', Defaults.img_format) |
6 | 236 |
|
237 |
if font_size > FONT_SIZE_MAX : |
|
238 |
raise ValueError(font_size) |
|
3 | 239 |
|
240 |
# put the chars in random order by default |
|
4 | 241 |
if req.args.get('random-chars', True, arg_bool) : |
3 | 242 |
chars = randomize(chars) |
243 |
||
244 |
# load/prep resources |
|
245 |
data = build_data(text, chars, colors) |
|
246 |
font = load_font(font_name, font_size) |
|
247 |
||
248 |
# render the image |
|
249 |
img = render_img(data, font, background_color, line_spacing) |
|
250 |
||
251 |
img = effect_sharpness(img, sharpness) |
|
252 |
||
7 | 253 |
png_data = build_img(img, img_format) |
3 | 254 |
|
255 |
# build the response |
|
7 | 256 |
response = werkzeug.Response(png_data, mimetype='image/%s' % img_format) |
3 | 257 |
|
258 |
return response |
|
259 |
||
260 |
@werkzeug.Request.application |
|
261 |
def wsgi_application (request) : |
|
0 | 262 |
""" |
3 | 263 |
Our werkzeug WSGI handler |
0 | 264 |
""" |
265 |
||
3 | 266 |
try : |
267 |
# request -> response |
|
268 |
response = handle_request(request) |
|
0 | 269 |
|
3 | 270 |
return response |
271 |
||
272 |
except HTTPException, e : |
|
273 |
# return as HTTP response |
|
274 |
return e |
|
0 | 275 |
|
276 |
def main () : |
|
3 | 277 |
handler = wsgiref.handlers.CGIHandler() |
278 |
app = wsgi_application |
|
279 |
||
6 | 280 |
if DEBUG : |
3 | 281 |
# enable debugging |
282 |
app = werkzeug.DebuggedApplication(app, evalex=False) |
|
0 | 283 |
|
3 | 284 |
handler.run(app) |
0 | 285 |
|
286 |
if __name__ == '__main__' : |
|
287 |
main() |
|
288 |