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