【问题标题】:"image contains error", trying to create and display images using google app engine“图像包含错误”,尝试使用谷歌应用引擎创建和显示图像
【发布时间】:2012-05-31 11:55:38
【问题描述】:

您好,总体思路是创建一个类似星系的地图。当我尝试显示生成的图像时遇到问题。我使用 Python Image 库创建图像并将其存储在数据存储中。

当我尝试加载图像时,日志控制台上没有错误,浏览器上也没有图像。 当我复制/粘贴图像链接(包括数据存储密钥)时,出现黑屏和以下消息:

图片 “查看源:/localhost:8080/img?img_id=ag5kZXZ-c3BhY2VzaW0xMnINCxIHTWFpbk1hcBgeDA” 无法显示,因为它包含错误。

firefox 错误控制台:

错误:图像损坏或截断: /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)

浏览器显示“找到的项目”和“找到的默认地图”字符串和损坏的图像链接 异常处理不会捕获任何错误

感谢您的帮助 问候 伯特

【问题讨论】:

    标签: python image google-app-engine google-cloud-datastore python-imaging-library


    【解决方案1】:

    PIL Image 对象到字符串的转换不是图像:

    >>> from PIL import Image
    >>> i = Image.new("RGBA", (100, 100))
    >>> str(i)
    '<PIL.Image.Image image mode=RGBA size=100x100 at 0x24C5DA0>'
    

    相反,您需要使用save() 函数并将其写入文件。在应用引擎的情况下,您使用字符串缓冲区:

    >>> import cStringIO
    >>> s = cStringIO.StringIO()
    >>> i.save(s, 'PNG')
    >>> dMap.defaultmap = db.Blob(s.getvalue())
    

    对于 ndb,当 dMap.defaultmap 是 ndb.BlobProperty:

    >>> import cStringIO
    >>> s = cStringIO.StringIO()
    >>> i.save(s, 'PNG')
    >>> dMap.defaultmap = s.getvalue()
    

    【讨论】:

      【解决方案2】:

      我怀疑你的图片是字符串

      >>> test = Image.new("RGBA",(100, 100))
      >>> test
      <Image.Image image mode=RGBA size=100x100 at 0x1005EC878>
      >>> str(test)
      '<Image.Image image mode=RGBA size=100x100 at 0x1005EC878>'
      

      Image 有一个 save 方法,可以将其写入文件类对象。

      如果defaultmap = blobstore.BlobReferenceProperty(),我认为以下(未经测试)可能适用于 blobstore

      from google.appengine.api import files
      file_name = files.blobstore.create(mime_type='image/png')
      with files.open(file_name, 'a') as f:
          test.save(f, format='PNG')
      files.finalize(file_name)
      blob_key = files.blobstore.get_blob_key(file_name)
      

      或者你可以例如实例化一个 StringIO 并保存到它,比如

      from StringIO import StringIO
      tmp = StringIO()
      test.save(tmp, format='PNG')
      dMap.defaultmap = db.Blob(tmp.getvalue())
      

      【讨论】:

      • 感谢您的意见。我已经尝试了您的建议,但第一个建议不起作用,因为 appengine 不允许任何写入(因此 test.save)操作。我收到以下错误:OSError:[Errno 13] 路径不可访问:'C:\\Python27\\DLLs'。当我使用数据存储时,第二个给了我完全相同的症状。
      • 我在 SDK 中本地尝试了这两个示例,它对我有用。请注意,我进行了一些编辑,添加了文件导入并在保存时设置格式。类文件对象是关键,它适用于应用程序引擎,因为我们不写入文件系统,而是写入提供 seek、tell 和 write 方法的对象。您对 PIL 导入有疑问吗?
      猜你喜欢
      • 1970-01-01
      • 2012-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-04
      相关资源
      最近更新 更多