Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added PerspectiveTransform #7699

Merged
merged 4 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions Tests/test_image_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,25 @@

class TestImageTransform:
def test_sanity(self):
im = Image.new("L", (100, 100))

seq = tuple(range(10))

transform = ImageTransform.AffineTransform(seq[:6])
im.transform((100, 100), transform)
transform = ImageTransform.ExtentTransform(seq[:4])
im.transform((100, 100), transform)
transform = ImageTransform.QuadTransform(seq[:8])
im.transform((100, 100), transform)
transform = ImageTransform.MeshTransform([(seq[:4], seq[:8])])
im.transform((100, 100), transform)
im = hopper()

for transform in (
ImageTransform.AffineTransform((1, 0, 0, 0, 1, 0)),
ImageTransform.PerspectiveTransform((1, 0, 0, 0, 1, 0, 0, 0)),
ImageTransform.ExtentTransform((0, 0) + im.size),
ImageTransform.QuadTransform(
(0, 0, 0, im.height, im.width, im.height, im.width, 0)
),
ImageTransform.MeshTransform(
[
(
(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):
comment = b"File written by Adobe Photoshop\xa8 4.0"
Expand Down
5 changes: 5 additions & 0 deletions docs/reference/ImageTransform.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ The :py:mod:`~PIL.ImageTransform` module contains implementations of
:undoc-members:
:show-inheritance:

.. autoclass:: PerspectiveTransform
:members:
:undoc-members:
:show-inheritance:

.. autoclass:: ExtentTransform
:members:
:undoc-members:
Expand Down
8 changes: 5 additions & 3 deletions docs/releasenotes/10.3.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ TODO
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
========
Expand Down
20 changes: 20 additions & 0 deletions src/PIL/ImageTransform.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,26 @@ class AffineTransform(Transform):
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):
"""
Define a transform to extract a subregion from an image.
Expand Down
Loading