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