close
The Wayback Machine - https://web.archive.org/web/20091220204528/http://code.google.com/appengine/docs/python/images/overview.html
My favoritesImage | ImageEnglishImage | Sign in

Images Python API Overview

App Engine provides the ability to manipulate image data using a dedicated Images service. The Images service can resize, rotate, flip, and crop images; it can composite multiple images into a single image; and it can convert image data between several formats. It can also enhance photographs using an predefined algorithm. The API can also provide information about an image, such as its format, width, height, and a histogram of color values.

The Images service can accept image data directly from the app, or it can use a Blobstore value. When the source is the Blobstore, the size of the image to transform can be up to the maximum size of a Blobstore value. However, the transformed image is returned directly to the app, and so must be no larger than 1 megabyte. This is potentially useful for making thumbnail images of photographs uploaded to the Blobstore by users.

Transforming Images in Python

The following example loads image data from the datastore, then uses the Images service to resize it and return it to the browser as a JPEG image.

from google.appengine.api import images

from google.appengine.ext import db
from google.appengine.ext import webapp

class Photo(db.Model):
    title = db.StringProperty()
    full_size_image = db.BlobProperty()

class Thumbnailer(webapp.RequestHandler):
    def get(self):
        if self.request.get("id"):
            photo = Photo.get_by_id(self.request.get("id"))

            if photo:
                img = images.Image(photo.full_size_image)
                img.resize(width=80, height=100)
                img.im_feeling_lucky()
                thumbnail = img.execute_transforms(output_encoding=images.JPEG)

                self.response.headers['Content-Type'] = 'image/jpeg'
                self.response.out.write(thumbnail)
                return

      # Either "id" wasn't provided, or there was no image with that ID
      # in the datastore.
      self.error(404)

Note: In order to use the Images API in your local environment you must first download and install PIL, the Python Imaging Library. PIL is not available on App Engine; it is only used as a stub for the Images API in your local environment. Only the transforms provided in the images API are available on App Engine.

Available Image Transformations

The Images service can resize, rotate, flip, and crop images, and enhance photographs. It can also composite multiple images into a single image.

Resize

You can resize the image while maintaining the same aspect ratio.

Image  Image

Rotate

You can rotate the image in 90 degree increments.

Image  Image

Flip Horizontally

You can flip the image horizontally.

Image  Image

Flip Vertically

You can flip the image vertically.

Image  Image

Crop

You can crop the image with a given bounding box.

Image  Image

I'm Feeling Lucky

The "I'm Feeling Lucky" transform enhances dark and bright colors in an image and adjusts both color and contrast to optimal levels.

Image  Image

Image Formats

The service accepts image data in the JPEG, PNG, GIF (including animated GIF), BMP, TIFF and ICO formats.

It can return transformed images in the JPEG and PNG formats. If the input format and the output format are different, the service converts the input data to the output format before performing the transformation.

Transforming Images from the Blobstore

The Images service can use a value from the Blobstore as the source for a transformation. An image from the Blobstore can be as large as the maximum size of a Blobstore value. Note, however, that the result of the transformation is returned directly to the app, and must therefore not exceed the API response limit of 1 megabyte. You can use this to make thumbnail images of photographs uploaded by users.

To transform an image from the Blobstore in Python, instead of setting the image_data argument of the Image constructor with the image data, set the blob_key argument to the Blobstore key whose value is the image. The rest of the API behaves as expected. The execute_transforms() method returns the result of the transforms, or raises a LargeImageError exception if the result is larger than the maximum size of 1 megabyte.

class Thumbnailer(webapp.RequestHandler):
    def get(self):
        blob_key = self.request.get("blob_key")
        if blob_key:
            blob_info = blobstore.get(blob_key)

            if blob_info:
                img = images.Image(blob_key=blob_key)
                img.resize(width=80, height=100)
                img.im_feeling_lucky()
                thumbnail = img.execute_transforms(output_encoding=images.JPEG)

                self.response.headers['Content-Type'] = 'image/jpeg'
                self.response.out.write(thumbnail)
                return

      # Either "blob_key" wasn't provided, or there was no value with that ID
      # in the Blobstore.
      self.error(404)

Images and the Development Server

The development server uses your local machine to perform the capabilities of the Images service.

The Python development server uses the Python Imaging Library (PIL) to simulate the Image service. This library is not included with the Python standard library or the SDK, and must be installed separately. See Installing PIL.

Quotas and Limits

Each Images service request counts toward the Image Manipulation API Calls quota. An app can perform multiple transformations of an image in a single API call.

Data sent to the Images service counts toward the Data Sent to (Images) API quota. Data received from the Images service counts toward the Data Received from (Images) API quota.

Each transformation of an image counts toward the Transformations Executed quota.

For more information on quotas, see Quotas, and the "Quota Details" section of the Admin Console.

In addition to quotas, the following limits apply to the use of the Images service:

Limit Amount
maximum data size of image sent to service 1 megabyte
maximum data size of image received from service 1 megabyte