"image contains error", trying to create and display images using google app engine

Posted by bert on Stack Overflow See other posts from Stack Overflow or by bert
Published on 2012-05-31T11:55:38Z Indexed on 2012/05/31 16:40 UTC
Read the original article Hit count: 311

Hello all the general idea is to create a galaxy-like map. I run into problems when I try to display a generated image. I used Python Image library to create the image and store it in the datastore.

when i try to load the image i get no error on the log console and no image on the browser. when i copy/paste the image link (including datastore key) i get a black screen and the following message:

The image “view-source:/localhost:8080/img?img_id=ag5kZXZ-c3BhY2VzaW0xMnINCxIHTWFpbk1hcBgeDA” cannot be displayed because it contains errors.

the firefox error console:

Error: Image corrupt or truncated: /localhost:8080/img?img_id=ag5kZXZ-c3BhY2VzaW0xMnINCxIHTWFpbk1hcBgeDA

import cgi
import datetime
import urllib
import webapp2
import jinja2
import os
import math
import sys



from google.appengine.ext import db
from google.appengine.api import users
from PIL import Image

#SNIP

#class to define the map entity
class MainMap(db.Model):
  defaultmap = db.BlobProperty(default=None)

#SNIP      


class Generator(webapp2.RequestHandler):
  def post(self):

        #SNIP

        test = Image.new("RGBA",(100, 100))
        dMap=MainMap()
        dMap.defaultmap = db.Blob(str(test))
        dMap.put()

        #SNIP

        result = db.GqlQuery("SELECT * FROM MainMap LIMIT 1").fetch(1)

        if result:
          print"item found<br>" #debug info

          if result[0].defaultmap:
              print"defaultmap found<br>" #debug info
              string = "<div><img src='/img?img_id=" + str(result[0].key()) + "' width='100' height='100'></img>"
              print string

        else:
            print"nothing found<br>"

    else:
        self.redirect('/?=error')
    self.redirect('/')


class Image_load(webapp2.RequestHandler):
    def get(self):
        self.response.out.write("started Image load")
        defaultmap = db.get(self.request.get("img_id"))
        if defaultmap.defaultmap:
            try:
              self.response.headers['Content-Type'] = "image/png"
              self.response.out.write(defaultmap.defaultmap)
              self.response.out.write("Image found")
            except:
              print "Unexpected error:", sys.exc_info()[0]
        else:
            self.response.out.write("No image")

#SNIP    



app = webapp2.WSGIApplication([('/', MainPage),
                               ('/generator', Generator),
                               ('/img', Image_load)],
                              debug=True)

the browser shows the "item found" and "defaultmap found" strings and a broken imagelink the exception handling does not catch any errors

Thanks for your help Regards Bert

© Stack Overflow or respective owner

Related posts about python

Related posts about image