CoolFace
Apppublic

Aluode/PerceptionLabPortable

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
ImageTransform.py137 linesDownload Raw Back to PIL
1#
2# The Python Imaging Library.
3# $Id$
4#
5# transform wrappers
6#
7# History:
8# 2002-04-08 fl   Created
9#
10# Copyright (c) 2002 by Secret Labs AB
11# Copyright (c) 2002 by Fredrik Lundh
12#
13# See the README file for information on usage and redistribution.
14#
15from __future__ import annotations
16
17from collections.abc import Sequence
18from typing import Any
19
20from . import Image
21
22
23class Transform(Image.ImageTransformHandler):
24    """Base class for other transforms defined in :py:mod:`~PIL.ImageTransform`."""
25
26    method: Image.Transform
27
28    def __init__(self, data: Sequence[Any]) -> None:
29        self.data = data
30
31    def getdata(self) -> tuple[Image.Transform, Sequence[int]]:
32        return self.method, self.data
33
34    def transform(
35        self,
36        size: tuple[int, int],
37        image: Image.Image,
38        **options: Any,
39    ) -> Image.Image:
40        """Perform the transform. Called from :py:meth:`.Image.transform`."""
41        # can be overridden
42        method, data = self.getdata()
43        return image.transform(size, method, data, **options)
44
45
46class AffineTransform(Transform):
47    """
48    Define an affine image transform.
49
50    This function takes a 6-tuple (a, b, c, d, e, f) which contain the first
51    two rows from the inverse of an affine transform matrix. For each pixel
52    (x, y) in the output image, the new value is taken from a position (a x +
53    b y + c, d x + e y + f) in the input image, rounded to nearest pixel.
54
55    This function can be used to scale, translate, rotate, and shear the
56    original image.
57
58    See :py:meth:`.Image.transform`
59
60    :param matrix: A 6-tuple (a, b, c, d, e, f) containing the first two rows
61        from the inverse of an affine transform matrix.
62    """
63
64    method = Image.Transform.AFFINE
65
66
67class PerspectiveTransform(Transform):
68    """
69    Define a perspective image transform.
70
71    This function takes an 8-tuple (a, b, c, d, e, f, g, h). For each pixel
72    (x, y) in the output image, the new value is taken from a position
73    ((a x + b y + c) / (g x + h y + 1), (d x + e y + f) / (g x + h y + 1)) in
74    the input image, rounded to nearest pixel.
75
76    This function can be used to scale, translate, rotate, and shear the
77    original image.
78
79    See :py:meth:`.Image.transform`
80
81    :param matrix: An 8-tuple (a, b, c, d, e, f, g, h).
82    """
83
84    method = Image.Transform.PERSPECTIVE
85
86
87class ExtentTransform(Transform):
88    """
89    Define a transform to extract a subregion from an image.
90
91    Maps a rectangle (defined by two corners) from the image to a rectangle of
92    the given size. The resulting image will contain data sampled from between
93    the corners, such that (x0, y0) in the input image will end up at (0,0) in
94    the output image, and (x1, y1) at size.
95
96    This method can be used to crop, stretch, shrink, or mirror an arbitrary
97    rectangle in the current image. It is slightly slower than crop, but about
98    as fast as a corresponding resize operation.
99
100    See :py:meth:`.Image.transform`
101
102    :param bbox: A 4-tuple (x0, y0, x1, y1) which specifies two points in the
103        input image's coordinate system. See :ref:`coordinate-system`.
104    """
105
106    method = Image.Transform.EXTENT
107
108
109class QuadTransform(Transform):
110    """
111    Define a quad image transform.
112
113    Maps a quadrilateral (a region defined by four corners) from the image to a
114    rectangle of the given size.
115
116    See :py:meth:`.Image.transform`
117
118    :param xy: An 8-tuple (x0, y0, x1, y1, x2, y2, x3, y3) which contain the
119        upper left, lower left, lower right, and upper right corner of the
120        source quadrilateral.
121    """
122
123    method = Image.Transform.QUAD
124
125
126class MeshTransform(Transform):
127    """
128    Define a mesh image transform.  A mesh transform consists of one or more
129    individual quad transforms.
130
131    See :py:meth:`.Image.transform`
132
133    :param data: A list of (bbox, quad) tuples.
134    """
135
136    method = Image.Transform.MESH
137 
Aluode/PerceptionLabPortable · CoolFace