GAE:如何获得blob-image高度 [英] GAE: How to get the blob-image height

查看:120
本文介绍了GAE:如何获得blob-image高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  avatar = db.BlobProperty()

通过调用图像实例属性的高度或宽度(

  height 

= profile.avatar.height

引发以下错误:


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


PIL如果图像存储在BlobProperty中,那么数据存储在数据存储中,如果<$ c $ c> profile 是您的实体,那么您可以以如下方式访问该高度:

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

如果图像在t中他blobstore(数据存储区中的blobstore.BlobReferenceProperty),那么你有2种方法来实现它,更好的方法是复杂的,并且需要获取blob的读取器并将其提供给exif reader以获取大小。一个简单的方法是:如果 avatar = db.BlobReferenceProperty() profile

/ code>是您的实体,那么:

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

#我们必须执行一个变换来访问宽度/高度
img.im_feeling_lucky()#做一个变换,否则GAE抱怨。

#将质量设置为1,如果图像很大,结果将适合1MB
img.execute_transforms(output_encoding = images.JPEG,quality = 1)

#现在你可以访问img.height和img.width


Given is the follwing model on GAE:

avatar = db.BlobProperty()

By calling the image instance properties height or width (see documentation) with:

height = profile.avatar.height

the following error is thrown:

AttributeError: 'Blob' object has no attribute 'height'

PIL is installed.

解决方案

If the image is stored in a BlobProperty, then the data is stored in the datastore, and if profile is your entity, then the height can be accessed as:

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

If the image is in the blobstore, (blobstore.BlobReferenceProperty in the datastore), then you have 2 ways of doing it, the better way is complicated and requires getting a reader for the blob and feeding it to a exif reader to get the size. An easier way is:

if avatar = db.BlobReferenceProperty() and profile is your entity, then:

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

这篇关于GAE:如何获得blob-image高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆