Pillow/src/PIL/ImageTransform.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

136 lines
3.8 KiB
Python
Raw Normal View History

2010-07-31 06:52:47 +04:00
#
# The Python Imaging Library.
# $Id$
#
# transform wrappers
#
# History:
# 2002-04-08 fl Created
#
# Copyright (c) 2002 by Secret Labs AB
# Copyright (c) 2002 by Fredrik Lundh
#
# See the README file for information on usage and redistribution.
#
from __future__ import annotations
2010-07-31 06:52:47 +04:00
2024-05-07 16:59:20 +03:00
from typing import Any, Sequence
2024-01-01 11:22:01 +03:00
from . import Image
2010-07-31 06:52:47 +04:00
2014-08-26 17:47:10 +04:00
2010-07-31 06:52:47 +04:00
class Transform(Image.ImageTransformHandler):
2024-01-02 16:52:12 +03:00
"""Base class for other transforms defined in :py:mod:`~PIL.ImageTransform`."""
method: Image.Transform
2024-01-01 11:22:01 +03:00
def __init__(self, data: Sequence[int]) -> None:
2010-07-31 06:52:47 +04:00
self.data = data
2014-08-26 17:47:10 +04:00
2024-01-02 16:52:12 +03:00
def getdata(self) -> tuple[Image.Transform, Sequence[int]]:
2010-07-31 06:52:47 +04:00
return self.method, self.data
2014-08-26 17:47:10 +04:00
2024-01-01 11:22:01 +03:00
def transform(
self,
size: tuple[int, int],
image: Image.Image,
2024-05-07 16:59:20 +03:00
**options: Any,
2024-01-01 11:22:01 +03:00
) -> Image.Image:
2024-01-02 16:52:12 +03:00
"""Perform the transform. Called from :py:meth:`.Image.transform`."""
2010-07-31 06:52:47 +04:00
# can be overridden
method, data = self.getdata()
return image.transform(size, method, data, **options)
2014-08-26 17:47:10 +04:00
2010-07-31 06:52:47 +04:00
class AffineTransform(Transform):
2016-05-24 10:36:14 +03:00
"""
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.
2024-01-02 16:52:12 +03:00
See :py:meth:`.Image.transform`
:param matrix: A 6-tuple (a, b, c, d, e, f) containing the first two rows
2016-05-24 10:36:14 +03:00
from an affine transform matrix.
"""
2019-03-21 16:28:20 +03:00
2022-01-15 01:02:31 +03:00
method = Image.Transform.AFFINE
2010-07-31 06:52:47 +04:00
2014-08-26 17:47:10 +04:00
2024-01-07 11:34:27 +03:00
class PerspectiveTransform(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 :py:meth:`.Image.transform`
:param matrix: An 8-tuple (a, b, c, d, e, f, g, h).
"""
method = Image.Transform.PERSPECTIVE
2010-07-31 06:52:47 +04:00
class ExtentTransform(Transform):
2016-05-24 10:36:14 +03:00
"""
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.
2024-01-02 16:52:12 +03:00
See :py:meth:`.Image.transform`
:param bbox: A 4-tuple (x0, y0, x1, y1) which specifies two points in the
input image's coordinate system. See :ref:`coordinate-system`.
2016-05-24 10:36:14 +03:00
"""
2019-03-21 16:28:20 +03:00
2022-01-15 01:02:31 +03:00
method = Image.Transform.EXTENT
2010-07-31 06:52:47 +04:00
2014-08-26 17:47:10 +04:00
2010-07-31 06:52:47 +04:00
class QuadTransform(Transform):
2016-05-24 10:36:14 +03:00
"""
Define a quad image transform.
Maps a quadrilateral (a region defined by four corners) from the image to a
rectangle of the given size.
2024-01-02 16:52:12 +03:00
See :py:meth:`.Image.transform`
2017-10-13 11:55:53 +03:00
:param xy: An 8-tuple (x0, y0, x1, y1, x2, y2, x3, y3) which contain the
2016-05-24 10:36:14 +03:00
upper left, lower left, lower right, and upper right corner of the
source quadrilateral.
"""
2019-03-21 16:28:20 +03:00
2022-01-15 01:02:31 +03:00
method = Image.Transform.QUAD
2010-07-31 06:52:47 +04:00
2014-08-26 17:47:10 +04:00
2010-07-31 06:52:47 +04:00
class MeshTransform(Transform):
2016-05-24 10:36:14 +03:00
"""
Define a mesh image transform. A mesh transform consists of one or more
individual quad transforms.
2024-01-02 16:52:12 +03:00
See :py:meth:`.Image.transform`
:param data: A list of (bbox, quad) tuples.
2016-05-24 10:36:14 +03:00
"""
2019-03-21 16:28:20 +03:00
2022-01-15 01:02:31 +03:00
method = Image.Transform.MESH