GAE:如何获得blob-image高度

时间:2011-01-25 17:27:43

标签: python google-app-engine blob python-imaging-library

鉴于GAE的后续模型:

avatar = db.BlobProperty()

通过以下方式调用图像实例属性高度或宽度(see documentation

height = profile.avatar.height

抛出以下错误:

  

AttributeError:'Blob'对象没有属性'height'

已安装PIL。

2 个答案:

答案 0 :(得分:13)

如果图像存储在BlobProperty中,则数据存储在数据存储区中,如果profile是您的实体,则可以按以下方式访问高度:

from google.appengine.api import images
height = images.Image(image_data=profile.avatar).height

如果图像位于blobstore中(数据存储区中的blobstore.BlobReferenceProperty),那么你有两种方法可以做到这一点,更好的方法是复杂的,需要获取一个读取器的blob并将其提供给exif阅读器得到大小。一种更简单的方法是:

如果avatar = db.BlobReferenceProperty()profile是您的实体,那么:

from google.appengine.api import images
img = images.Image(blob_key=str(profile.avatar.key()))

# we must execute a transform to access the width/height
img.im_feeling_lucky() # do a transform, otherwise GAE complains.

# set quality to 1 so the result will fit in 1MB if the image is huge
img.execute_transforms(output_encoding=images.JPEG,quality=1)

# now you can access img.height and img.width

答案 1 :(得分:5)

blob不是图像,而是一块数据。

要从blob中生成Image,如果blob存储在blobstore中,则必须调用Image(blob_key=your_blob_key);如果将blob存储为blob,则必须调用Image(image_data=your_image_data)数据存储。

相关问题