ImageTransform.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. #
  15. from __future__ import annotations
  16. from . import Image
  17. TYPE_CHECKING = False
  18. if TYPE_CHECKING:
  19. from collections.abc import Sequence
  20. from typing import Any
  21. class Transform(Image.ImageTransformHandler):
  22. """Base class for other transforms defined in :py:mod:`~PIL.ImageTransform`."""
  23. method: Image.Transform
  24. def __init__(self, data: Sequence[Any]) -> None:
  25. self.data = data
  26. def getdata(self) -> tuple[Image.Transform, Sequence[int]]:
  27. return self.method, self.data
  28. def transform(
  29. self,
  30. size: tuple[int, int],
  31. image: Image.Image,
  32. **options: Any,
  33. ) -> Image.Image:
  34. """Perform the transform. Called from :py:meth:`.Image.transform`."""
  35. # can be overridden
  36. method, data = self.getdata()
  37. return image.transform(size, method, data, **options)
  38. class AffineTransform(Transform):
  39. """
  40. Define an affine image transform.
  41. This function takes a 6-tuple (a, b, c, d, e, f) which contain the first
  42. two rows from the inverse of an affine transform matrix. For each pixel
  43. (x, y) in the output image, the new value is taken from a position (a x +
  44. b y + c, d x + e y + f) in the input image, rounded to nearest pixel.
  45. This function can be used to scale, translate, rotate, and shear the
  46. original image.
  47. See :py:meth:`.Image.transform`
  48. :param matrix: A 6-tuple (a, b, c, d, e, f) containing the first two rows
  49. from the inverse of an affine transform matrix.
  50. """
  51. method = Image.Transform.AFFINE
  52. class PerspectiveTransform(Transform):
  53. """
  54. Define a perspective image transform.
  55. This function takes an 8-tuple (a, b, c, d, e, f, g, h). For each pixel
  56. (x, y) in the output image, the new value is taken from a position
  57. ((a x + b y + c) / (g x + h y + 1), (d x + e y + f) / (g x + h y + 1)) in
  58. the input image, rounded to nearest pixel.
  59. This function can be used to scale, translate, rotate, and shear the
  60. original image.
  61. See :py:meth:`.Image.transform`
  62. :param matrix: An 8-tuple (a, b, c, d, e, f, g, h).
  63. """
  64. method = Image.Transform.PERSPECTIVE
  65. class ExtentTransform(Transform):
  66. """
  67. Define a transform to extract a subregion from an image.
  68. Maps a rectangle (defined by two corners) from the image to a rectangle of
  69. the given size. The resulting image will contain data sampled from between
  70. the corners, such that (x0, y0) in the input image will end up at (0,0) in
  71. the output image, and (x1, y1) at size.
  72. This method can be used to crop, stretch, shrink, or mirror an arbitrary
  73. rectangle in the current image. It is slightly slower than crop, but about
  74. as fast as a corresponding resize operation.
  75. See :py:meth:`.Image.transform`
  76. :param bbox: A 4-tuple (x0, y0, x1, y1) which specifies two points in the
  77. input image's coordinate system. See :ref:`coordinate-system`.
  78. """
  79. method = Image.Transform.EXTENT
  80. class QuadTransform(Transform):
  81. """
  82. Define a quad image transform.
  83. Maps a quadrilateral (a region defined by four corners) from the image to a
  84. rectangle of the given size.
  85. See :py:meth:`.Image.transform`
  86. :param xy: An 8-tuple (x0, y0, x1, y1, x2, y2, x3, y3) which contain the
  87. upper left, lower left, lower right, and upper right corner of the
  88. source quadrilateral.
  89. """
  90. method = Image.Transform.QUAD
  91. class MeshTransform(Transform):
  92. """
  93. Define a mesh image transform. A mesh transform consists of one or more
  94. individual quad transforms.
  95. See :py:meth:`.Image.transform`
  96. :param data: A list of (bbox, quad) tuples.
  97. """
  98. method = Image.Transform.MESH