ImageTransform.py 2.8 KB

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