1

I'm currently making an image sharing site in Appengine, it's mostly working how I want it, but sometimes I would like a small version of an image (thumbnail) - at the moment I'm shrinking it with HTML, but the browser still loads the full (up to 32MB image) when that is entirely unnecessary. How do I show a lower quality, smaller image to save bandwidth?

Code for serving the image. This is 'mysite.com/view'

class ViewPhotoHandler(blobstore_handlers.BlobstoreDownloadHandler):
    def get(self, photo_key):
        if not blobstore.get(photo_key):
            self.error(404)
        else:
            self.send_blob(photo_key)

code for requesting the image.

p.blob_key is a string

link = '/view/%s' % p.blob_key
self.response.write('<img src="' + link + '" alt="Image" height="50" width="50">')

So, everything works how it should, but it wastes too much bandwidth when requesting the small version of the image.

Any ideas on how I can solve this? Thanks

Gary Kerr
  • 13,650
  • 4
  • 48
  • 51
Joseph
  • 193
  • 11

1 Answers1

2

Use the get_serving_url function from the images package

from google.appengine.api.images import get_serving_url

url = get_serving_url(blob_key, size=None, crop=False, secure_url=None)

You can resize the image, with or without cropping it.

Images API documentation: https://developers.google.com/appengine/docs/python/images/functions

Note: your handler now doesn't need to be a subclass of BlobstoreDownloadHandler.

Edit: dealing with error.

From the documentation: The Images API uses the Python Imaging Library (PIL) locally to transform images during testing on your local machine. You'll need to download the PIL module and install it on your local machine to use the Images API with the SDK.

Gary Kerr
  • 13,650
  • 4
  • 48
  • 51
  • Thanks for your help, but it didn't work, I got this error; line 68, in _transform_image image = _get_images_stub()._OpenImageData(image_data) AttributeError: 'ImagesNotImplementedServiceStub' object has no attribute '_OpenImageData' – Joseph May 10 '13 at 19:49
  • You need to install PIL for the images API to work on your local machine. See edit in answer. – Gary Kerr May 10 '13 at 20:00
  • Thanks, but PIL can't be installed when running Python 2.7, and I can't use 2.5, any suggestions? – Joseph May 10 '13 at 20:12
  • I'm using PIL with Python 2.7. What operating system are you using. – Gary Kerr May 10 '13 at 20:19
  • I'm using Mac OSX Mountain Lion – Joseph May 10 '13 at 20:21
  • What about this: http://stackoverflow.com/questions/14654781/installing-pil-on-osx-mountain-lion-for-google-app-engine – Gary Kerr May 10 '13 at 20:25
  • That didn't work either. But thanks, you've been a big help. I'm just deploying the app as a non-default version and testing it on the appengine servers rather than locally, and everything works great. – Joseph May 10 '13 at 20:39