ImageTransform.py 3.1 KB

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