GAE: How to get the blob-image height
Asked Answered
J

3

6

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.

Janejanean answered 25/1, 2011 at 17:27 Comment(0)
G
13

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
Guaranty answered 25/1, 2011 at 21:5 Comment(1)
can you also show the better/complicated method as well here?Operculum
B
5

A blob is not an image, it is a lump of data.

In order to make an Image out of your blob, you have to call Image(blob_key=your_blob_key) if your blob is stored in the blobstore, or Image(image_data=your_image_data) if it's stored as a blob in the datastore.

Bonnes answered 25/1, 2011 at 17:44 Comment(0)
C
0

To get image sizes without applying transformations, the image data can be fetched from the blobstore using a BlobKey and the size of the data, obtained form BlobInfo:

from google.appengine.api import blobstore
from google.appengine.api import images

# ...

image_data = blobstore.fetch_data(blob_key, 0, blob_info.size)
image = images.Image(image_data=image_data)

# image.width and image.height is accessible
Croesus answered 2/10, 2021 at 18:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.