ImageTransform.py 3.8 KB

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