The PIL.Image Module

The PIL.Image Module

blend(im1, im2, alpha) [#]

Creates a new image by interpolating between two input images, using a constant alpha.

out = image1 * (1.0 - alpha) + image2 * alpha

im1 im2 alpha Returns:

composite(image1, image2, mask) [#]

image1 image2 mask

eval(image, function) [#]

image function Returns:

frombuffer(mode, size, data, decoder_name=”raw”, *args) [#]

(New in 1.1.4) Creates an image memory from pixel data in a string or byte buffer.

This function is similar to **fromstring**, but uses data in the byte buffer, where possible. This means that changes to the original buffer object are reflected in this image). Not all modes can share memory; support modes include “L”, “RGBX”, “RGBA”, and “CMYK”. For other modes, this function behaves like a corresponding call to the fromstring function.

Note that this function decodes pixel data only, not entire images. If you have an entire image file in a string, wrap it in a StringIO object, and use **open** to load it.

mode size data decoder_name *args Returns:

fromstring(mode, size, data, decoder_name=”raw”, *args) [#]

Creates an image memory from pixel data in a string.

In its simplest form, this function takes three arguments (mode, size, and unpacked pixel data).

You can also use any pixel decoder supported by PIL. For more information on available decoders, see the section *Writing Your Own File Decoder*.

Note that this function decodes pixel data only, not entire images. If you have an entire image in a string, wrap it in a StringIO object, and use **open** to load it.

mode size data decoder_name *args Returns:

getmodebandnames(mode) [#]

Gets a list of individual band names. Given a mode, this function returns a tuple containing the names of individual bands (use **getmodetype** to get the mode used to store each individual band.

mode Returns: Raises KeyError:

getmodebands(mode) [#]

mode Returns: Raises KeyError:

getmodebase(mode) [#]

mode Returns: Raises KeyError:

getmodetype(mode) [#]

mode Returns: Raises KeyError:
Image() (class) [#]

This class represents an image object.

For more information about this class, see *The Image Class*.

init() [#] isDirectory(f) [#] isImageType(t) [#] isStringType(t) [#] isTupleType(t) [#] merge(mode, bands) [#]

mode bands Returns:

new(mode, size, color=0) [#]

mode size color Returns:
open(file, mode=”r”) [#]

Opens and identifies the given image file.

This is a lazy operation; this function identifies the file, but the actual image data is not read from the file until you try to process the data (or call the **load** method).

A filename (string) or a file object. The file object must implement read, seek, and tell methods, and be opened in binary mode.

mode Returns: Raises IOError:

preinit() [#] register_extension(id, extension) [#]

id extension

register_mime(id, mimetype) [#]

id mimetype

register_open(id, factory, accept=None) [#]

id factory accept

register_save(id, driver) [#]

id driver

The Image Class

Image() (class) [#] convert(mode, matrix=None) [#]

Returns a converted copy of this image. For the “P” mode, this method translates pixels through the palette. If mode is omitted, a mode is chosen so that all information in the image and the palette can be represented without a palette.

The current version supports all possible conversions between “L”, “RGB” and “CMYK.”

When translating a colour image to black and white (mode “L”), the library uses the ITU-R 601-2 luma transform:

L = R * 299/1000 + G * 587/1000 + B * 114/1000

When translating a greyscale image into a bilevel image (mode “1”), all non-zero values are set to 255 (white). To use other thresholds, use the **point** method.

mode matrix Returns:

copy() [#]

crop(box=None) [#]

Returns a rectangular region from this image. The box is a 4-tuple defining the left, upper, right, and lower pixel coordinate.

This is a lazy operation. Changes to the source image may or may not be reflected in the cropped image. To break the connection, call the **load** method on the cropped copy.

The Returns:

draft(mode, size) [#]

Configures the image file loader so it returns a version of the image that as closely as possible matches the given mode and size. For example, you can use this method to convert a colour JPEG to greyscale while loading it, or to extract a 128x192 version from a PCD file.

Note that this method modifies the Image object in place. If the image has already been loaded, this method has no effect.

mode size

filter(filter) [#]

Filters this image using the given filter. For a list of available filters, see the ImageFilter module.

filter Returns:

fromstring(data, decoder_name=”raw”, *args) [#]

Loads this image with pixel data from a string.

This method is similar to the **fromstring** function, but loads data into this image instead of creating a new image object.

getbands() [#]

Returns a tuple containing the name of each band in this image. For example, getbands on an RGB image returns (“R”, “G”, “B”).


getbbox() [#]


getcolors(maxcolors=256) [#]

maxcolors Returns:
getdata(band=None) [#]

Returns the contents of this image as a sequence object containing pixel values. The sequence object is flattened, so that values for line one follow directly after the values of line zero, and so on.

Note that the sequence object returned by this method is an internal PIL data type, which only supports certain sequence operations. To convert it to an ordinary sequence (e.g. for printing), use list(im.getdata()).

band Returns:

getextrema() [#]


getim() [#]


getpalette() [#]


getpixel(xy) [#]

xy Returns:

getprojection() [#]

histogram(mask=None) [#]

Returns a histogram for the image. The histogram is returned as a list of pixel counts, one for each pixel value in the source image. If the image has more than one band, the histograms for all bands are concatenated (for example, the histogram for an “RGB” image contains 768 values).

A bilevel image (mode “1”) is treated as a greyscale (“L”) image by this method.

If a mask is provided, the method returns a histogram for those parts of the image where the mask image is non-zero. The mask image must have the same size as the image, and be either a bi-level image (mode “1”) or a greyscale image (“L”).

mask Returns:

load() [#]


offset(xoffset, yoffset=None) [#]

(Deprecated) Returns a copy of the image where the data has been offset by the given distances. Data wraps around the edges. If yoffset is omitted, it is assumed to be equal to xoffset.

This method is deprecated. New code should use the offset function in the ImageChops module.

xoffset yoffset Returns:

paste(im, box=None, mask=None) [#]

Pastes another image into this image. The box argument is either a 2-tuple giving the upper left corner, a 4-tuple defining the left, upper, right, and lower pixel coordinate, or None (same as (0, 0)). If a 4-tuple is given, the size of the pasted image must match the size of the region.

If the modes don’t match, the pasted image is converted to the mode of this image (see the **convert** method for details).

Instead of an image, the source can be a integer or tuple containing pixel values. The method then fills the region with the given colour. When creating RGB images, you can also use colour strings as supported by the ImageColor module.

If a mask is given, this method updates only the regions indicated by the mask. You can use either “1”, “L” or “RGBA” images (in the latter case, the alpha band is used as mask). Where the mask is 255, the given image is copied as is. Where the mask is 0, the current value is preserved. Intermediate values can be used for transparency effects.

Note that if you paste an “RGBA” image, the alpha band is ignored. You can work around this by using the same image as both source image and mask.

im box

An optional 4-tuple giving the region to paste into. If a 2-tuple is used instead, it’s treated as the upper left corner. If omitted or None, the source is pasted into the upper left corner.

If an image is given as the second argument and there is no third, the box defaults to (0, 0), and the second argument is interpreted as a mask image.

mask Returns:

point(lut, mode=None) [#]

lut mode Returns:

putalpha(alpha) [#]


putdata(data, scale=1.0, offset=0.0) [#]

Copies pixel data to this image. This method copies data from a sequence object into the image, starting at the upper left corner (0, 0), and continuing until either the image or the sequence ends. The scale and offset values are used to adjust the sequence values: pixel = value*scale + offset.

data scale offset

putpalette(data) [#]

putpixel(xy, value) [#]

Modifies the pixel at the given position. The colour is given as a single numerical value for single-band images, and a tuple for multi-band images.

Note that this method is relatively slow. For more extensive changes, use **paste** or the ImageDraw module instead.

xy value

resize(size, filter=NEAREST) [#]

size filter

An optional resampling filter. This can be one of NEAREST (use nearest neighbour), BILINEAR (linear interpolation in a 2x2 environment), BICUBIC (cubic spline interpolation in a 4x4 environment), or ANTIALIAS (a high-quality downsampling filter). If omitted, or if the image has mode “1” or “P”, it is set NEAREST.


rotate(angle, filter=NEAREST) [#]

angle filter

An optional resampling filter. This can be one of NEAREST (use nearest neighbour), BILINEAR (linear interpolation in a 2x2 environment), or BICUBIC (cubic spline interpolation in a 4x4 environment). If omitted, or if the image has mode “1” or “P”, it is set NEAREST.


save(file, format=None, **options) [#]

Saves this image under the given filename. If no format is specified, the format to use is determined from the filename extension, if possible.

Keyword options can be used to provide additional instructions to the writer. If a writer doesn’t recognise an option, it is silently ignored. The available options are described later in this handbook.

You can use a file object instead of a filename. In this case, you must always specify the format. The file object must implement the seek, tell, and write methods, and be opened in binary mode.

file format **options Returns: Raises KeyError: Raises IOError:

seek(frame) [#]

Seeks to the given frame in this sequence file. If you seek beyond the end of the sequence, the method raises an EOFError exception. When a sequence file is opened, the library automatically seeks to frame 0.

Note that in the current version of the library, most sequence formats only allows you to seek to the next frame.

frame Raises EOFError:

show(title=None) [#]

Displays this image. This method is mainly intended for debugging purposes.

On Unix platforms, this method saves the image to a temporary PPM file, and calls the xv utility.

On Windows, it saves the image to a temporary BMP file, and uses the standard BMP display utility to show it (usually Paint).


split() [#]


tell() [#]


thumbnail(size, resample=NEAREST) [#]

Make this image into a thumbnail. This method modifies the image to contain a thumbnail version of itself, no larger than the given size. This method calculates an appropriate thumbnail size to preserve the aspect of the image, calls the **draft** method to configure the file reader (where applicable), and finally resizes the image.

Note that the bilinear and bicubic filters in the current version of PIL are not well-suited for thumbnail generation. You should use ANTIALIAS unless speed is much more important than quality.

Also note that this function modifies the Image object in place. If you need to use the full resolution image as well, apply this method to a **copy** of the original image.

size resample

Optional resampling filter. This can be one of NEAREST, BILINEAR, BICUBIC, or ANTIALIAS (best quality). If omitted, it defaults to NEAREST (this will be changed to ANTIALIAS in a future version).


tobitmap(name=”image”) [#]

name Returns: Raises ValueError:

tostring(encoder_name=”raw”, *args) [#]

encoder_name *args Returns:

transform(size, method, data, resample=NEAREST) [#]

Transforms this image. This method creates a new image with the given size, and the same mode as the original, and copies data to the new image using the given transform.

size method

The transformation method. This is one of EXTENT (cut out a rectangular subregion), AFFINE (affine transform), PERSPECTIVE (perspective transform), QUAD (map a quadrilateral to a rectangle), or MESH (map a number of source quadrilaterals in one operation).

data resample

Optional resampling filter. It can be one of NEAREST (use nearest neighbour), BILINEAR (linear interpolation in a 2x2 environment), or BICUBIC (cubic spline interpolation in a 4x4 environment). If omitted, or if the image has mode “1” or “P”, it is set to NEAREST.


transpose(method) [#]


verify() [#]