Please note that the contents of this offline web site may be out of date. To access the most recent documentation visit the online version .
Note that links that point to online resources are green in color and will open in a new window.
We would love it if you could give us feedback about this material by filling this form (You have to be online to fill it)



Images Python API Overview

Python | Java | PHP | Go

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 a 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 or a Google Cloud Storage value. When the source is the Blobstore or Google Cloud Storage, the size of the image to transform can be up to the maximum size of a Blobstore value or Google Cloud Storage value. However, the transformed image is returned directly to the app, and so must be no larger than 32 megabytes. This is potentially useful for making thumbnail images of photographs uploaded to the Blobstore or Google Cloud Storage by users.

  1. Transforming images in Python
  2. Available image transformations
  3. Image formats
  4. Transforming images from the Blobstore
  5. Images and the development server
  6. Quotas and limits

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.

import webapp2
from google.appengine.api import images
from google.appengine.ext import db

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

class Thumbnailer(webapp2.RequestHandler):
    def get(self):
        if self.request.get("id"):
            photo = Photo.get_by_id(int(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)

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. Neither the width nor the height of the resized image can exceed 4000 pixels.

Rotate

You can rotate the image in 90 degree increments.

Flip horizontally

You can flip the image horizontally.

Flip vertically

You can flip the image vertically.

Crop

You can crop the image with a given bounding box.

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 formats

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

It can return transformed images in the JPEG, WEBP 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. You have two ways to transform images from the Blobstore:

  1. Using the Image() class allows you to perform simple image transformations, such as crop, flip, and rotate.
  2. Using get_serving_url() allows you to dynamically resize and crop images, so you don't need to store different image sizes on the server. This method returns a URL that serves the image, and transformations to the image are encoded in this URL.

Using the Image() Class

You can transform images from the Blobstore as long as the image size is smaller than the maximum Blobstore value size. Note, however, that the result of the transformation is returned directly to the app, and must therefore not exceed the API response limit of 32 megabytes. 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 throws an LargeImageError if the result is larger than the maximum size of 32 megabytes.

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

class Thumbnailer(webapp2.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)

Using get_serving_url()

The get_serving_url() method allows you to generate a stable, dedicated URL for serving web-suitable image thumbnails. You simply store a single copy of your original image in Blobstore, and then request a high-performance per-image URL. This special URL can serve that image resized and/or cropped automatically, and serving from this URL does not incur any CPU or dynamic serving load on your application (though bandwidth is still charged as usual). Images are served with low latency from a highly optimized, cookieless infrastructure.

The URL returned by this method is always public, but not guessable; private URLs are not currently supported. If you wish to stop serving the URL, delete it using the delete_serving_url function.

If you supply the arguments, this method returns a URL encoded with the arguments specified. If you do not supply any arguments, this method returns the default URL for the image, for example:

http://your_app_id.appspot.com/randomStringImageId

You can then add arguments to this URL to get the desired size and crop parameters. The available arguments are:

  • =sxx where xx is an integer from 0–1600 representing the length, in pixels, of the image's longest side. For example, adding =s32 resizes the image so its longest dimension is 32 pixels.
  • =sxx-c where xx is an integer from 0–1600 representing the cropped image size in pixels, and -c tells the system to crop the image.
// Resize the image to 32 pixels (aspect-ratio preserved)
http://your_app_id.appspot.com/randomStringImageId=s32

// Crop the image to 32 pixels
http://your_app_id.appspot.com/randomStringImageId=s32-c

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 . The WEBP image format is only supported if a suitable PIL decoder plugin has been installed.

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 32 megabytes
maximum data size of image received from service 32 megabytes
maximum size of image sent or received from service 50 megapixels

Authentication required

You need to be signed in with Google+ to do that.

Signing you in...

Google Developers needs your permission to do that.