Commits

Chris Beelby  committed 995a3e7

Testing dynamic image generation with PIL

  • Participants
  • Parent commits 307a204

Comments (0)

Files changed (1)

File upfront/views.py

 
 @view_config(context=Root, name='image')
 def image_test(context, request):
-    resp = Response(content_type='image/jpeg')
-    resp.app_iter = open('/home/chris/env/upfront/upfront/static/images/headerphoto.jpg', 'rb')
+    resp = Response(content_type='image/png')
+    import ImageDraw, ImageFont, StringIO
+    from PIL import Image
+    
+    image = Image.new('RGB', (400, 100), (150, 150, 150))
+    draw = ImageDraw.Draw(image)
+    fontfile = '/usr/share/fonts/truetype/ttf-liberation/LiberationSans-Regular.ttf'
+    font25 = ImageFont.truetype(fontfile, 25)
+    font16 = ImageFont.truetype(fontfile, 16)
+    draw.text((10, 10), 'Hello, World!', font=font25, fill=(255, 255, 255))
+    draw.text((10, 50), 'Nice Buffer!', font=font16, fill=(0, 0, 0))
+    data = StringIO.StringIO()
+    image.save(data, 'PNG')
+    resp.app_iter = data.getvalue()
     return resp
 
 @view_config(context=User, renderer='upfront:templates/users/games.mako', name='games')