ImageTransform.py 3.8 KB

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