ImageTransform Module

The ImageTransform module contains implementations of ImageTransformHandler for some of the builtin Image.Transform methods.

class PIL.ImageTransform.Transform(data: Sequence[int])[source]

Bases: ImageTransformHandler

Base class for other transforms defined in ImageTransform.

getdata() tuple[Transform, Sequence[int]][source]
method: Transform
transform(size: tuple[int, int], image: Image, **options: dict[str, str | int | tuple[int, ...] | list[int]]) Image[source]

Perform the transform. Called from Image.transform().

class PIL.ImageTransform.AffineTransform(data: Sequence[int])[source]

Bases: Transform

Define an affine image transform.

This function takes a 6-tuple (a, b, c, d, e, f) which contain the first two rows from an affine transform matrix. For each pixel (x, y) in the output image, the new value is taken from a position (a x + b y + c, d x + e y + f) in the input image, rounded to nearest pixel.

This function can be used to scale, translate, rotate, and shear the original image.

See Image.transform()

Parameters:

matrix – A 6-tuple (a, b, c, d, e, f) containing the first two rows from an affine transform matrix.

method: Transform = 0
class PIL.ImageTransform.PerspectiveTransform(data: Sequence[int])[source]

Bases: Transform

Define a perspective image transform.

This function takes an 8-tuple (a, b, c, d, e, f, g, h). For each pixel (x, y) in the output image, the new value is taken from a position ((a x + b y + c) / (g x + h y + 1), (d x + e y + f) / (g x + h y + 1)) in the input image, rounded to nearest pixel.

This function can be used to scale, translate, rotate, and shear the original image.

See Image.transform()

Parameters:

matrix – An 8-tuple (a, b, c, d, e, f, g, h).

method: Transform = 2
class PIL.ImageTransform.ExtentTransform(data: Sequence[int])[source]

Bases: Transform

Define a transform to extract a subregion from an image.

Maps a rectangle (defined by two corners) from the image to a rectangle of the given size. The resulting image will contain data sampled from between the corners, such that (x0, y0) in the input image will end up at (0,0) in the output image, and (x1, y1) at size.

This method can be used to crop, stretch, shrink, or mirror an arbitrary rectangle in the current image. It is slightly slower than crop, but about as fast as a corresponding resize operation.

See Image.transform()

Parameters:

bbox – A 4-tuple (x0, y0, x1, y1) which specifies two points in the input image’s coordinate system. See Coordinate System.

method: Transform = 1
class PIL.ImageTransform.QuadTransform(data: Sequence[int])[source]

Bases: Transform

Define a quad image transform.

Maps a quadrilateral (a region defined by four corners) from the image to a rectangle of the given size.

See Image.transform()

Parameters:

xy – An 8-tuple (x0, y0, x1, y1, x2, y2, x3, y3) which contain the upper left, lower left, lower right, and upper right corner of the source quadrilateral.

method: Transform = 3
class PIL.ImageTransform.MeshTransform(data: Sequence[int])[source]

Bases: Transform

Define a mesh image transform. A mesh transform consists of one or more individual quad transforms.

See Image.transform()

Parameters:

data – A list of (bbox, quad) tuples.

method: Transform = 4