Merge pull request #7699 from radarhere/perspective

This commit is contained in:
Hugo van Kemenade 2024-01-09 14:30:42 +02:00 committed by GitHub
commit 35d6a6608f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 48 additions and 14 deletions

View File

@ -10,18 +10,25 @@ from .helper import assert_image_equal, assert_image_similar, hopper
class TestImageTransform: class TestImageTransform:
def test_sanity(self): def test_sanity(self):
im = Image.new("L", (100, 100)) im = hopper()
seq = tuple(range(10)) for transform in (
ImageTransform.AffineTransform((1, 0, 0, 0, 1, 0)),
transform = ImageTransform.AffineTransform(seq[:6]) ImageTransform.PerspectiveTransform((1, 0, 0, 0, 1, 0, 0, 0)),
im.transform((100, 100), transform) ImageTransform.ExtentTransform((0, 0) + im.size),
transform = ImageTransform.ExtentTransform(seq[:4]) ImageTransform.QuadTransform(
im.transform((100, 100), transform) (0, 0, 0, im.height, im.width, im.height, im.width, 0)
transform = ImageTransform.QuadTransform(seq[:8]) ),
im.transform((100, 100), transform) ImageTransform.MeshTransform(
transform = ImageTransform.MeshTransform([(seq[:4], seq[:8])]) [
im.transform((100, 100), transform) (
(0, 0) + im.size,
(0, 0, 0, im.height, im.width, im.height, im.width, 0),
)
]
),
):
assert_image_equal(im, im.transform(im.size, transform))
def test_info(self): def test_info(self):
comment = b"File written by Adobe Photoshop\xa8 4.0" comment = b"File written by Adobe Photoshop\xa8 4.0"

View File

@ -19,6 +19,11 @@ The :py:mod:`~PIL.ImageTransform` module contains implementations of
:undoc-members: :undoc-members:
:show-inheritance: :show-inheritance:
.. autoclass:: PerspectiveTransform
:members:
:undoc-members:
:show-inheritance:
.. autoclass:: ExtentTransform .. autoclass:: ExtentTransform
:members: :members:
:undoc-members: :undoc-members:

View File

@ -26,10 +26,12 @@ TODO
API Additions API Additions
============= =============
TODO Added PerspectiveTransform
^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^
TODO :py:class:`~PIL.ImageTransform.PerspectiveTransform` has been added, meaning
that all of the :py:data:`~PIL.Image.Transform` values now have a corresponding
subclass of :py:class:`~PIL.ImageTransform.Transform`.
Security Security
======== ========

View File

@ -63,6 +63,26 @@ class AffineTransform(Transform):
method = Image.Transform.AFFINE method = Image.Transform.AFFINE
class PerspectiveTransform(Transform):
"""
Define a perspective image transform.
This function takes an 8-tuple (a, b, c, d, e, f, g, h). For each pixel
(x, y) in the output image, the new value is taken from a position
((a x + b y + c) / (g x + h y + 1), (d x + e y + f) / (g x + h y + 1)) in
the input image, rounded to nearest pixel.
This function can be used to scale, translate, rotate, and shear the
original image.
See :py:meth:`.Image.transform`
:param matrix: An 8-tuple (a, b, c, d, e, f, g, h).
"""
method = Image.Transform.PERSPECTIVE
class ExtentTransform(Transform): class ExtentTransform(Transform):
""" """
Define a transform to extract a subregion from an image. Define a transform to extract a subregion from an image.