为什么这个图像没有显示(图像图标被破坏)?

时间:2011-09-25 13:42:58

标签: python image google-app-engine

我正在尝试使用下面的DisplayImage处理程序显示我保存到数据存储区的图像,但我只看到图像链接损坏。你知道为什么吗?谢谢!

class HomePage(db.Model):
    thumbnail = db.BlobProperty()
    firm_name = db.StringProperty()
...

class ImageUpload(webapp.RequestHandler):
    def get(self):
...        
        self.response.out.write("""
        <form action="/imagesave" enctype="multipart/form-data" method="post">
        <div><label>firm name:</label> <input type="text" name="firm_name" size=40></div>
        <div><input type="file" name="img" /></div>
        <div><input type="submit" value="Upload image"></div>
        </form>
        """)

class ImageSave(webapp.RequestHandler):
   def post(self):
        homepage = HomePage()
        thumbnail = self.request.get("img")
        firm_name = self.request.get("firm_name")
        homepage.thumbnail = db.Blob(thumbnail)
        homepage.firm_name = firm_name
        homepage.put()

        self.redirect("/imageupload")

class ImageResize(webapp.RequestHandler):
    def post(self):
        q = HomepageImage.all()
        q.filter("firm_name", "mta")
        qTable = q.get()

        if qTable:
            qTable.thumbnail = db.Blob(images.resize(self.request.get("img"), 32, 32))
            db.put(qTable)
        else:
            self.response.out.write("""firm not found""")
        self.redirect("/imageupload")

class DisplayImage(webapp.RequestHandler):
    def get(self):       
        query = HomePage.all()
        query.filter("firm_name", "mta")
        result = query.get()

        self.response.out.write("""firm name: %s""" % result.firm_name)

        self.response.out.write("""<img src="img?img_id=%s"></img>""" %
        result.key())
...

2 个答案:

答案 0 :(得分:1)

要从blobstore提供图像,请使用get_serving_url,或者如果您有blobproperty,您可以查看我之前用于在没有blobstore之前从图像类提供blobproperties的旧代码: / p>

class Image(db.Model):
    name        = db.StringProperty()
    desc        = db.StringProperty()
    owner       = db.UserProperty()
    secret      = db.StringProperty()
    full        = db.BlobProperty()
    full_ext    = db.StringProperty()
    small       = db.BlobProperty()
    small_ext   = db.StringProperty()
    thumb       = db.BlobProperty()
    thumb_ext   = db.StringProperty()
    published   = db.BooleanProperty()
    added       = db.DateTimeProperty(auto_now_add=True)
    modified    = db.DateTimeProperty(auto_now=True)

    def thumb_name(self):
        return '%s.%s' % (self.key(), self.thumb_ext)
    def small_name(self):
        return '%s_small.%s' % (self.key(), self.small_ext)
    def full_name(self):
        return '%s_full.%s' % (self.key(), self.full_ext)



class UploadImage(webapp.RequestHandler):
    def post(self, key):
        im = db.get(db.Key(key))
        if not im:
            self.error(404)
            return

        if self.request.POST['id'] != im.secret:
            self.error(400)
            return

        file_data = self.request.POST['file'].file.read()

        if self.request.POST['size'] == '100x100':
            im.thumb = file_data
            a = 'small'
        elif self.request.POST['size'] == '500x500':
            im.small = file_data
            a = 'full'

        if im.small and im.thumb:
            im.published = True
        im.save()
        logging.info("%s updated %s" % (im.key(), a) )
        self.response.out.write("ok")

mimetypes = {
    'jpeg': 'image/jpeg',
    'jpg':  'image/jpeg',
    'tiff':  'image/tiff',
    'tif':   'image/tiff',
    'gif':   'image/gif',
    'png':   'image/png',
    }

class ServeImage(webapp.RequestHandler):
    def get(self, key, sz, ext):
        im = db.get(db.Key(key))
        if not im:
            self.error(404)
            return

        if sz == '.':
            d = im.thumb
        elif sz == '_small.':
            d = im.small
        elif sz == '_full.':
            d = im.full
        else:
            raise Exception('wrong sz %r' % sz)

        if not d:
            d = im.full
        else:
            self.response.headers.add_header("Expires", "Thu, 01 Dec 2014 16:00:00 GMT")

        self.response.headers["Content-Type"] = mimetypes[ext]
        self.response.out.write(d)

答案 1 :(得分:0)

我的followup question systempuntoout已回答了这个问题。他指出,我将图像源指向未定义的错误img路径。

正确的链接应指向/ image,如下所示:

<img src="/image?img_id=%s"></img&GT;