implement image-format
authorTero Marttila <terom@fixme.fi>
Tue, 05 May 2009 17:32:27 +0300
changeset 7 9a6ac93e7446
parent 6 f61000aa264b
child 8 44d5ead35f4b
implement image-format
index.cgi
--- a/index.cgi	Tue May 05 17:25:04 2009 +0300
+++ b/index.cgi	Tue May 05 17:32:27 2009 +0300
@@ -27,12 +27,12 @@
 class Defaults :
     # settings
     text = [
-        "aalto",
-        "unive",
-        "rsity"
+        u"aalto",
+        u"unive",
+        u"rsity"
     ]
 
-    chars = [ '"', '!', '?' ]
+    chars = [ u'"', u'!', u'?' ]
 
     line_colors = [
         "#0469af",
@@ -47,6 +47,8 @@
     line_spacing = -10
     sharpness = 0.6
 
+    img_format = 'png'
+
 fonts = {
         'dejavu-sans-bold':     "/usr/share/fonts/truetype/ttf-dejavu/DejaVuSans-Bold.ttf",
         'helvetica':            "HELR65W.TTF",
@@ -177,14 +179,16 @@
 
     return ImageEnhance.Sharpness(img).enhance(factor)
 
-def build_png (img) :
+def build_img (img, format='png') :
     """
         Write the given PIL.Image as a string, returning the raw binary data
+
+        Format should be one of the PIL-supported image foarts
     """
 
     # render PNG output
     buf = StringIO()
-    img.save(buf, "png")
+    img.save(buf, format)
     data = buf.getvalue()
 
     return data
@@ -228,6 +232,7 @@
     background_color = req.args.get('background-color', Defaults.background_color, arg_color)
     line_spacing = req.args.get('line-spacing', Defaults.line_spacing, int)
     sharpness = req.args.get('sharpness', Defaults.sharpness, float)
+    img_format = req.args.get('image-format', Defaults.img_format)
 
     if font_size > FONT_SIZE_MAX :
         raise ValueError(font_size)
@@ -245,10 +250,10 @@
 
     img = effect_sharpness(img, sharpness)
 
-    png_data = build_png(img)
+    png_data = build_img(img, img_format)
     
     # build the response
-    response = werkzeug.Response(png_data, mimetype='image/png')
+    response = werkzeug.Response(png_data, mimetype='image/%s' % img_format)
 
     return response