GAE: how to get blob image height

. The following GAE model is introduced:

avatar = db.BlobProperty() 

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

 height = profile.avatar.height 

The following error is issued:

AttributeError: Blob has no height attribute

Installed PIL.

+6
python google-app-engine blob python-imaging-library
source share
2 answers

If the image is stored in BlobProperty, the data is stored in the data warehouse, 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 a block block (blobstore.BlobReferenceProperty in the data store), then you have 2 ways to do this, the best way is complicated and requires getting a reader for the blob and passing it to the exif reader to get the size. A simpler way:

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 
+13
source share

BLOB is not an image, it is a piece of data.

To make Image from your blob, you must call Image(blob_key=your_blob_key) if your blob is stored in the blob, or Image(image_data=your_image_data) if it is stored in a block in the data warehouse.

+5
source share

All Articles