App Engine, PIL and overlaying text
Question: I'm trying to overlay some text over an image on GAE. Now they expose the PIL
library it should not be problem.

Here's what I have. It works, but I can't help think I should be writing
directly to the background image rather than creating a separate overlay image
and then merging.

Can I use
[Image.frombuffer](http://www.pythonware.com/library/pil/handbook/image.htm)
or something, I've given it a go but I'm just not getting it...

    
    
    from PIL import Image, ImageDraw, ImageFont
    from google.appengine.api import images
    from google.appengine.ext import blobstore
    from google.appengine.api import files
    
    def compose(key):
    
        # create new image
        text_img = Image.new('RGBA', (800,600), (0, 0, 0, 0))
        draw = ImageDraw.Draw(text_img)
        draw.text((0, 0), 'HELLO TEXT', font=ImageFont.load_default())
    
        # no write access on GAE
        output = StringIO.StringIO()
        text_img.save(output, format="png")
        text_layer = output.getvalue()
        output.close()
    
        # read background image
        blob_reader = blobstore.BlobReader(key)
        background = images.Image(blob_reader.read())
    
        # merge
        merged = images.composite([(background, 0, 0, 1.0, images.TOP_LEFT), 
                                   (text_layer, 0, 0, 1.0, images.TOP_LEFT)], 
                                   800, 600)
    
        # save
        file_name = files.blobstore.create(mime_type='image/png')
        with files.open(file_name, 'a') as f:
            f.write(merged)
        files.finalize(file_name)
    


Answer: You should use the `[Image.open][1]` method instead. `Image.frombuffer` and
`Image.fromstring` decode pixel data not raw images.

In your case you could use something like:

    
    
    blob_reader = blobstore.BlobReader(key)
    text_img = Image.open(blob_reader)
    .........
    

