gpt4 book ai didi

python - GAE : How to get the blob-image height

转载 作者:太空狗 更新时间:2023-10-29 22:05:12 24 4
gpt4 key购买 nike

给出的是 GAE 上的以下模型:

avatar = db.BlobProperty()

通过调用图像实例属性高度或宽度(see documentation):

height = profile.avatar.height

抛出以下错误:

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

PIL 已安装。

最佳答案

如果图像存储在 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

关于python - GAE : How to get the blob-image height,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4796793/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com