【发布时间】:2012-11-28 10:46:45
【问题描述】:
使用 Python 图像库 PIL 和 Google App Engine Blobstore...
这个:
img = images.Image(blob_key=image)
logging.info(img.size)
self.response.headers['Content-Type'] = 'image/jpeg'
self.response.out.write(img)
有属性错误:
AttributeError: 'Image' object has no attribute 'size'
所以来自谷歌应用引擎的 Image 实例没有大小?
那么这是如何工作的:
img = images.Image(blob_key=image)
img.resize(width, height)
img.im_feeling_lucky()
thumbnail = img.execute_transforms(output_encoding=images.JPEG)
self.response.headers['Content-Type'] = 'image/jpeg'
self.response.out.write(thumbnail)
我错过了什么?
编辑:
修复是使用 get_serving_url 而不是使用我的图像服务器,正如@voscausa 所建议的那样。 由于我的对象是由 jinja2 模板解析的,因此不可能通过 jinja2 创建 Image 对象。 所以最终的解决方案如下:
class Mandelbrot(db.Model):
image = blobstore.BlobReferenceProperty()
@property
def image_url(self):
return images.get_serving_url(self.image)
这样我可以将图像 url 解析到我的页面,例如:
<img src=
{% if mandelbrot.image %}
"{{ mandelbrot.image_url }}"
{% else %}
"./assets/img/preloader.gif"
{% endif %}
/>
【问题讨论】:
-
再解释一下。没有调整大小的第一种方法不提供有效的图像资源。这是问题,而不是大小。
-
您的问题确实与 PIL 无关,您应该更改标题。 Google 可能会使用 PIL 来实现其 Image 类,但它是一个完全独立的东西。
标签: python google-app-engine blobstore