2023-12-21 14:13:31 +03:00
|
|
|
from __future__ import annotations
|
2024-01-20 14:23:03 +03:00
|
|
|
|
2016-07-10 21:47:59 +03:00
|
|
|
import math
|
2024-02-12 01:28:53 +03:00
|
|
|
from typing import Callable
|
2016-07-10 21:47:59 +03:00
|
|
|
|
2020-02-22 16:06:21 +03:00
|
|
|
import pytest
|
2020-08-07 13:28:33 +03:00
|
|
|
|
2019-10-10 12:22:41 +03:00
|
|
|
from PIL import Image, ImageTransform
|
2012-10-16 00:26:38 +04:00
|
|
|
|
2020-03-22 22:54:54 +03:00
|
|
|
from .helper import assert_image_equal, assert_image_similar, hopper
|
2019-07-06 23:40:53 +03:00
|
|
|
|
2013-10-01 08:57:19 +04:00
|
|
|
|
2020-03-22 22:54:54 +03:00
|
|
|
class TestImageTransform:
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_sanity(self) -> None:
|
2024-01-08 02:36:30 +03:00
|
|
|
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))
|
2014-07-20 01:51:26 +04:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_info(self) -> None:
|
2019-10-10 12:22:41 +03:00
|
|
|
comment = b"File written by Adobe Photoshop\xa8 4.0"
|
|
|
|
|
2019-11-25 23:03:23 +03:00
|
|
|
with Image.open("Tests/images/hopper.gif") as im:
|
2020-02-22 16:06:21 +03:00
|
|
|
assert im.info["comment"] == comment
|
2019-10-10 12:22:41 +03:00
|
|
|
|
2019-11-25 23:03:23 +03:00
|
|
|
transform = ImageTransform.ExtentTransform((0, 0, 0, 0))
|
|
|
|
new_im = im.transform((100, 100), transform)
|
2020-02-22 16:06:21 +03:00
|
|
|
assert new_im.info["comment"] == comment
|
2019-10-10 12:22:41 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_palette(self) -> None:
|
2021-07-30 13:11:05 +03:00
|
|
|
with Image.open("Tests/images/hopper.gif") as im:
|
2022-01-15 01:02:31 +03:00
|
|
|
transformed = im.transform(
|
|
|
|
im.size, Image.Transform.AFFINE, [1, 0, 0, 0, 1, 0]
|
|
|
|
)
|
2021-07-30 13:11:05 +03:00
|
|
|
assert im.palette.palette == transformed.palette.palette
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_extent(self) -> None:
|
2019-06-13 18:54:24 +03:00
|
|
|
im = hopper("RGB")
|
2014-06-10 13:10:47 +04:00
|
|
|
(w, h) = im.size
|
2022-10-09 10:57:15 +03:00
|
|
|
transformed = im.transform(
|
|
|
|
im.size,
|
|
|
|
Image.Transform.EXTENT,
|
|
|
|
(0, 0, w // 2, h // 2), # ul -> lr
|
|
|
|
Image.Resampling.BILINEAR,
|
|
|
|
)
|
2014-06-10 13:10:47 +04:00
|
|
|
|
2022-01-15 01:02:31 +03:00
|
|
|
scaled = im.resize((w * 2, h * 2), Image.Resampling.BILINEAR).crop((0, 0, w, h))
|
2014-06-10 13:10:47 +04:00
|
|
|
|
|
|
|
# undone -- precision?
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_similar(transformed, scaled, 23)
|
2014-06-03 14:02:44 +04:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_quad(self) -> None:
|
2014-06-10 13:10:47 +04:00
|
|
|
# one simple quad transform, equivalent to scale & crop upper left quad
|
2019-06-13 18:54:24 +03:00
|
|
|
im = hopper("RGB")
|
2014-06-10 13:10:47 +04:00
|
|
|
(w, h) = im.size
|
2022-10-09 10:57:15 +03:00
|
|
|
transformed = im.transform(
|
|
|
|
im.size,
|
|
|
|
Image.Transform.QUAD,
|
|
|
|
(0, 0, 0, h // 2, w // 2, h // 2, w // 2, 0), # ul -> ccw around quad
|
|
|
|
Image.Resampling.BILINEAR,
|
|
|
|
)
|
2014-06-03 14:02:44 +04:00
|
|
|
|
2019-06-13 18:54:24 +03:00
|
|
|
scaled = im.transform(
|
2022-01-15 01:02:31 +03:00
|
|
|
(w, h),
|
|
|
|
Image.Transform.AFFINE,
|
|
|
|
(0.5, 0, 0, 0, 0.5, 0),
|
|
|
|
Image.Resampling.BILINEAR,
|
2019-06-13 18:54:24 +03:00
|
|
|
)
|
2013-10-01 08:57:19 +04:00
|
|
|
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_equal(transformed, scaled)
|
2014-06-03 14:02:44 +04:00
|
|
|
|
2022-08-24 15:43:49 +03:00
|
|
|
@pytest.mark.parametrize(
|
2022-08-29 19:35:06 +03:00
|
|
|
"mode, expected_pixel",
|
2022-08-24 15:43:49 +03:00
|
|
|
(
|
|
|
|
("RGB", (255, 0, 0)),
|
|
|
|
("RGBA", (255, 0, 0, 255)),
|
|
|
|
("LA", (76, 0)),
|
|
|
|
),
|
|
|
|
)
|
2024-02-12 01:28:53 +03:00
|
|
|
def test_fill(self, mode: str, expected_pixel: tuple[int, ...]) -> None:
|
2022-08-24 15:43:49 +03:00
|
|
|
im = hopper(mode)
|
|
|
|
(w, h) = im.size
|
|
|
|
transformed = im.transform(
|
|
|
|
im.size,
|
|
|
|
Image.Transform.EXTENT,
|
|
|
|
(0, 0, w * 2, h * 2),
|
|
|
|
Image.Resampling.BILINEAR,
|
|
|
|
fillcolor="red",
|
|
|
|
)
|
2022-08-29 19:35:06 +03:00
|
|
|
assert transformed.getpixel((w - 1, h - 1)) == expected_pixel
|
2017-11-11 19:31:37 +03:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_mesh(self) -> None:
|
2014-09-05 14:03:56 +04:00
|
|
|
# this should be a checkerboard of halfsized hoppers in ul, lr
|
2019-06-13 18:54:24 +03:00
|
|
|
im = hopper("RGBA")
|
2014-06-10 13:10:47 +04:00
|
|
|
(w, h) = im.size
|
2022-10-09 10:57:15 +03:00
|
|
|
transformed = im.transform(
|
|
|
|
im.size,
|
|
|
|
Image.Transform.MESH,
|
|
|
|
(
|
|
|
|
(
|
|
|
|
(0, 0, w // 2, h // 2), # box
|
|
|
|
(0, 0, 0, h, w, h, w, 0), # ul -> ccw around quad
|
|
|
|
),
|
|
|
|
(
|
|
|
|
(w // 2, h // 2, w, h), # box
|
|
|
|
(0, 0, 0, h, w, h, w, 0), # ul -> ccw around quad
|
|
|
|
),
|
|
|
|
),
|
|
|
|
Image.Resampling.BILINEAR,
|
|
|
|
)
|
2014-06-03 14:02:44 +04:00
|
|
|
|
2019-06-13 18:54:24 +03:00
|
|
|
scaled = im.transform(
|
2022-01-15 01:02:31 +03:00
|
|
|
(w // 2, h // 2),
|
|
|
|
Image.Transform.AFFINE,
|
|
|
|
(2, 0, 0, 0, 2, 0),
|
|
|
|
Image.Resampling.BILINEAR,
|
2019-06-13 18:54:24 +03:00
|
|
|
)
|
2013-10-01 09:01:23 +04:00
|
|
|
|
2019-06-13 18:54:24 +03:00
|
|
|
checker = Image.new("RGBA", im.size)
|
2014-06-10 13:10:47 +04:00
|
|
|
checker.paste(scaled, (0, 0))
|
2019-06-13 18:54:24 +03:00
|
|
|
checker.paste(scaled, (w // 2, h // 2))
|
2013-10-01 09:10:19 +04:00
|
|
|
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_equal(transformed, checker)
|
2013-10-01 09:01:23 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# now, check to see that the extra area is (0, 0, 0, 0)
|
2019-06-13 18:54:24 +03:00
|
|
|
blank = Image.new("RGBA", (w // 2, h // 2), (0, 0, 0, 0))
|
2014-06-03 14:02:44 +04:00
|
|
|
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_equal(blank, transformed.crop((w // 2, 0, w, h // 2)))
|
|
|
|
assert_image_equal(blank, transformed.crop((0, h // 2, w // 2, h)))
|
2013-10-01 09:01:23 +04:00
|
|
|
|
2024-02-12 01:28:53 +03:00
|
|
|
def _test_alpha_premult(
|
|
|
|
self, op: Callable[[Image.Image, tuple[int, int]], Image.Image]
|
|
|
|
) -> None:
|
2014-06-10 13:10:47 +04:00
|
|
|
# create image with half white, half black,
|
|
|
|
# with the black half transparent.
|
|
|
|
# do op,
|
|
|
|
# there should be no darkness in the white section.
|
2019-06-13 18:54:24 +03:00
|
|
|
im = Image.new("RGBA", (10, 10), (0, 0, 0, 0))
|
|
|
|
im2 = Image.new("RGBA", (5, 10), (255, 255, 255, 255))
|
2014-06-10 13:10:47 +04:00
|
|
|
im.paste(im2, (0, 0))
|
2013-10-01 09:10:19 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
im = op(im, (40, 10))
|
2019-06-13 18:54:24 +03:00
|
|
|
im_background = Image.new("RGB", (40, 10), (255, 255, 255))
|
2014-06-10 13:10:47 +04:00
|
|
|
im_background.paste(im, (0, 0), im)
|
2013-10-01 09:01:23 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
hist = im_background.histogram()
|
2020-02-22 16:06:21 +03:00
|
|
|
assert 40 * 10 == hist[-1]
|
2014-06-03 14:02:44 +04:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_alpha_premult_resize(self) -> None:
|
2024-02-12 01:28:53 +03:00
|
|
|
def op(im: Image.Image, sz: tuple[int, int]) -> Image.Image:
|
2022-01-15 01:02:31 +03:00
|
|
|
return im.resize(sz, Image.Resampling.BILINEAR)
|
2013-10-04 23:55:25 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
self._test_alpha_premult(op)
|
2014-06-03 14:02:44 +04:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_alpha_premult_transform(self) -> None:
|
2024-02-12 01:28:53 +03:00
|
|
|
def op(im: Image.Image, sz: tuple[int, int]) -> Image.Image:
|
2014-06-10 13:10:47 +04:00
|
|
|
(w, h) = im.size
|
2022-01-15 01:02:31 +03:00
|
|
|
return im.transform(
|
|
|
|
sz, Image.Transform.EXTENT, (0, 0, w, h), Image.Resampling.BILINEAR
|
|
|
|
)
|
2014-06-03 14:02:44 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
self._test_alpha_premult(op)
|
2014-06-03 14:02:44 +04:00
|
|
|
|
2024-02-12 01:28:53 +03:00
|
|
|
def _test_nearest(
|
|
|
|
self, op: Callable[[Image.Image, tuple[int, int]], Image.Image], mode: str
|
|
|
|
) -> None:
|
2021-03-03 15:58:13 +03:00
|
|
|
# create white image with half transparent,
|
|
|
|
# do op,
|
2021-03-27 04:06:36 +03:00
|
|
|
# the image should remain white with half transparent
|
2021-03-03 15:58:13 +03:00
|
|
|
transparent, opaque = {
|
|
|
|
"RGBA": ((255, 255, 255, 0), (255, 255, 255, 255)),
|
|
|
|
"LA": ((255, 0), (255, 255)),
|
|
|
|
}[mode]
|
|
|
|
im = Image.new(mode, (10, 10), transparent)
|
|
|
|
im2 = Image.new(mode, (5, 10), opaque)
|
|
|
|
im.paste(im2, (0, 0))
|
|
|
|
|
|
|
|
im = op(im, (40, 10))
|
|
|
|
|
2024-07-05 20:56:24 +03:00
|
|
|
colors = im.getcolors()
|
|
|
|
assert colors is not None
|
|
|
|
assert sorted(colors) == sorted(
|
2022-10-09 10:58:14 +03:00
|
|
|
(
|
|
|
|
(20 * 10, opaque),
|
|
|
|
(20 * 10, transparent),
|
|
|
|
)
|
|
|
|
)
|
2021-03-03 15:58:13 +03:00
|
|
|
|
|
|
|
@pytest.mark.parametrize("mode", ("RGBA", "LA"))
|
2024-02-12 01:28:53 +03:00
|
|
|
def test_nearest_resize(self, mode: str) -> None:
|
|
|
|
def op(im: Image.Image, sz: tuple[int, int]) -> Image.Image:
|
2022-01-15 01:02:31 +03:00
|
|
|
return im.resize(sz, Image.Resampling.NEAREST)
|
2021-03-03 15:58:13 +03:00
|
|
|
|
|
|
|
self._test_nearest(op, mode)
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("mode", ("RGBA", "LA"))
|
2024-02-12 01:28:53 +03:00
|
|
|
def test_nearest_transform(self, mode: str) -> None:
|
|
|
|
def op(im: Image.Image, sz: tuple[int, int]) -> Image.Image:
|
2021-03-03 15:58:13 +03:00
|
|
|
(w, h) = im.size
|
2022-01-15 01:02:31 +03:00
|
|
|
return im.transform(
|
|
|
|
sz, Image.Transform.EXTENT, (0, 0, w, h), Image.Resampling.NEAREST
|
|
|
|
)
|
2021-03-03 15:58:13 +03:00
|
|
|
|
|
|
|
self._test_nearest(op, mode)
|
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_blank_fill(self) -> None:
|
2014-06-10 13:10:47 +04:00
|
|
|
# attempting to hit
|
|
|
|
# https://github.com/python-pillow/Pillow/issues/254 reported
|
|
|
|
#
|
|
|
|
# issue is that transforms with transparent overflow area
|
|
|
|
# contained junk from previous images, especially on systems with
|
|
|
|
# constrained memory. So, attempt to fill up memory with a
|
|
|
|
# pattern, free it, and then run the mesh test again. Using a 1Mp
|
|
|
|
# image with 4 bands, for 4 megs of data allocated, x 64. OMM (64
|
|
|
|
# bit 12.04 VM with 512 megs available, this fails with Pillow <
|
|
|
|
# a0eaf06cc5f62a6fb6de556989ac1014ff3348ea
|
|
|
|
#
|
|
|
|
# Running by default, but I'd totally understand not doing it in
|
|
|
|
# the future
|
2014-06-03 14:02:44 +04:00
|
|
|
|
2024-02-12 01:28:53 +03:00
|
|
|
pattern: list[Image.Image] | None = [
|
|
|
|
Image.new("RGBA", (1024, 1024), (a, a, a, a)) for a in range(1, 65)
|
|
|
|
]
|
2013-10-04 23:55:25 +04:00
|
|
|
|
2014-06-10 13:10:47 +04:00
|
|
|
# Yeah. Watch some JIT optimize this out.
|
2018-10-18 21:49:14 +03:00
|
|
|
pattern = None # noqa: F841
|
2014-06-10 13:10:47 +04:00
|
|
|
|
|
|
|
self.test_mesh()
|
2013-10-04 23:55:25 +04:00
|
|
|
|
2024-01-31 12:12:58 +03:00
|
|
|
def test_missing_method_data(self) -> None:
|
Improve handling of file resources
Follow Python's file object semantics. User code is responsible for
closing resources (usually through a context manager) in a deterministic
way.
To achieve this, remove __del__ functions. These functions used to
closed open file handlers in an attempt to silence Python
ResourceWarnings. However, using __del__ has the following drawbacks:
- __del__ isn't called until the object's reference count reaches 0.
Therefore, resource handlers remain open or in use longer than
necessary.
- The __del__ method isn't guaranteed to execute on system exit. See the
Python documentation:
https://docs.python.org/3/reference/datamodel.html#object.__del__
> It is not guaranteed that __del__() methods are called for objects
> that still exist when the interpreter exits.
- Exceptions that occur inside __del__ are ignored instead of raised.
This has the potential of hiding bugs. This is also in the Python
documentation:
> Warning: Due to the precarious circumstances under which __del__()
> methods are invoked, exceptions that occur during their execution
> are ignored, and a warning is printed to sys.stderr instead.
Instead, always close resource handlers when they are no longer in use.
This will close the file handler at a specified point in the user's code
and not wait until the interpreter chooses to. It is always guaranteed
to run. And, if an exception occurs while closing the file handler, the
bug will not be ignored.
Now, when code receives a ResourceWarning, it will highlight an area
that is mishandling resources. It should not simply be silenced, but
fixed by closing resources with a context manager.
All warnings that were emitted during tests have been cleaned up. To
enable warnings, I passed the `-Wa` CLI option to Python. This exposed
some mishandling of resources in ImageFile.__init__() and
SpiderImagePlugin.loadImageSeries(), they too were fixed.
2019-05-25 19:30:58 +03:00
|
|
|
with hopper() as im:
|
2020-02-22 16:06:21 +03:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
im.transform((100, 100), None)
|
2017-01-28 06:21:41 +03:00
|
|
|
|
2022-08-24 15:43:49 +03:00
|
|
|
@pytest.mark.parametrize("resample", (Image.Resampling.BOX, "unknown"))
|
2024-02-12 01:28:53 +03:00
|
|
|
def test_unknown_resampling_filter(self, resample: Image.Resampling | str) -> None:
|
Improve handling of file resources
Follow Python's file object semantics. User code is responsible for
closing resources (usually through a context manager) in a deterministic
way.
To achieve this, remove __del__ functions. These functions used to
closed open file handlers in an attempt to silence Python
ResourceWarnings. However, using __del__ has the following drawbacks:
- __del__ isn't called until the object's reference count reaches 0.
Therefore, resource handlers remain open or in use longer than
necessary.
- The __del__ method isn't guaranteed to execute on system exit. See the
Python documentation:
https://docs.python.org/3/reference/datamodel.html#object.__del__
> It is not guaranteed that __del__() methods are called for objects
> that still exist when the interpreter exits.
- Exceptions that occur inside __del__ are ignored instead of raised.
This has the potential of hiding bugs. This is also in the Python
documentation:
> Warning: Due to the precarious circumstances under which __del__()
> methods are invoked, exceptions that occur during their execution
> are ignored, and a warning is printed to sys.stderr instead.
Instead, always close resource handlers when they are no longer in use.
This will close the file handler at a specified point in the user's code
and not wait until the interpreter chooses to. It is always guaranteed
to run. And, if an exception occurs while closing the file handler, the
bug will not be ignored.
Now, when code receives a ResourceWarning, it will highlight an area
that is mishandling resources. It should not simply be silenced, but
fixed by closing resources with a context manager.
All warnings that were emitted during tests have been cleaned up. To
enable warnings, I passed the `-Wa` CLI option to Python. This exposed
some mishandling of resources in ImageFile.__init__() and
SpiderImagePlugin.loadImageSeries(), they too were fixed.
2019-05-25 19:30:58 +03:00
|
|
|
with hopper() as im:
|
|
|
|
(w, h) = im.size
|
2022-08-24 15:43:49 +03:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
im.transform((100, 100), Image.Transform.EXTENT, (0, 0, w, h), resample)
|
2019-05-20 23:08:57 +03:00
|
|
|
|
2013-10-01 09:01:23 +04:00
|
|
|
|
2020-03-22 22:54:54 +03:00
|
|
|
class TestImageTransformAffine:
|
2022-01-15 01:02:31 +03:00
|
|
|
transform = Image.Transform.AFFINE
|
2016-07-11 00:26:12 +03:00
|
|
|
|
2024-02-12 01:28:53 +03:00
|
|
|
def _test_image(self) -> Image.Image:
|
2019-06-13 18:54:24 +03:00
|
|
|
im = hopper("RGB")
|
2016-07-10 14:59:36 +03:00
|
|
|
return im.crop((10, 20, im.width - 10, im.height - 20))
|
|
|
|
|
2022-08-24 15:43:49 +03:00
|
|
|
@pytest.mark.parametrize(
|
2022-08-29 19:35:06 +03:00
|
|
|
"deg, transpose",
|
2022-08-24 15:43:49 +03:00
|
|
|
(
|
|
|
|
(0, None),
|
|
|
|
(90, Image.Transpose.ROTATE_90),
|
|
|
|
(180, Image.Transpose.ROTATE_180),
|
|
|
|
(270, Image.Transpose.ROTATE_270),
|
|
|
|
),
|
|
|
|
)
|
2024-02-12 01:28:53 +03:00
|
|
|
def test_rotate(self, deg: int, transpose: Image.Transpose | None) -> None:
|
2016-07-10 14:59:36 +03:00
|
|
|
im = self._test_image()
|
|
|
|
|
2019-06-13 18:54:24 +03:00
|
|
|
angle = -math.radians(deg)
|
2016-07-10 21:47:59 +03:00
|
|
|
matrix = [
|
2019-06-13 18:54:24 +03:00
|
|
|
round(math.cos(angle), 15),
|
|
|
|
round(math.sin(angle), 15),
|
|
|
|
0.0,
|
|
|
|
round(-math.sin(angle), 15),
|
|
|
|
round(math.cos(angle), 15),
|
|
|
|
0.0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
]
|
2016-07-10 21:47:59 +03:00
|
|
|
matrix[2] = (1 - matrix[0] - matrix[1]) * im.width / 2
|
|
|
|
matrix[5] = (1 - matrix[3] - matrix[4]) * im.height / 2
|
|
|
|
|
|
|
|
if transpose is not None:
|
|
|
|
transposed = im.transpose(transpose)
|
|
|
|
else:
|
|
|
|
transposed = im
|
2016-07-11 00:47:58 +03:00
|
|
|
|
2022-01-15 01:02:31 +03:00
|
|
|
for resample in [
|
|
|
|
Image.Resampling.NEAREST,
|
|
|
|
Image.Resampling.BILINEAR,
|
|
|
|
Image.Resampling.BICUBIC,
|
|
|
|
]:
|
2019-06-13 18:54:24 +03:00
|
|
|
transformed = im.transform(
|
|
|
|
transposed.size, self.transform, matrix, resample
|
|
|
|
)
|
2020-01-30 17:56:07 +03:00
|
|
|
assert_image_equal(transposed, transformed)
|
2016-07-10 14:59:36 +03:00
|
|
|
|
2022-08-24 15:43:49 +03:00
|
|
|
@pytest.mark.parametrize(
|
2022-08-29 19:35:06 +03:00
|
|
|
"scale, epsilon_scale",
|
2022-08-24 15:43:49 +03:00
|
|
|
(
|
|
|
|
(1.1, 6.9),
|
|
|
|
(1.5, 5.5),
|
|
|
|
(2.0, 5.5),
|
|
|
|
(2.3, 3.7),
|
|
|
|
(2.5, 3.7),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"resample,epsilon",
|
|
|
|
(
|
|
|
|
(Image.Resampling.NEAREST, 0),
|
|
|
|
(Image.Resampling.BILINEAR, 2),
|
|
|
|
(Image.Resampling.BICUBIC, 1),
|
|
|
|
),
|
|
|
|
)
|
2024-02-12 01:28:53 +03:00
|
|
|
def test_resize(
|
|
|
|
self,
|
|
|
|
scale: float,
|
|
|
|
epsilon_scale: float,
|
|
|
|
resample: Image.Resampling,
|
|
|
|
epsilon: int,
|
|
|
|
) -> None:
|
2016-07-10 16:07:31 +03:00
|
|
|
im = self._test_image()
|
2016-07-11 00:47:58 +03:00
|
|
|
|
|
|
|
size_up = int(round(im.width * scale)), int(round(im.height * scale))
|
2019-06-13 18:54:24 +03:00
|
|
|
matrix_up = [1 / scale, 0, 0, 0, 1 / scale, 0, 0, 0]
|
|
|
|
matrix_down = [scale, 0, 0, 0, scale, 0, 0, 0]
|
|
|
|
|
2022-08-24 15:43:49 +03:00
|
|
|
transformed = im.transform(size_up, self.transform, matrix_up, resample)
|
|
|
|
transformed = transformed.transform(
|
|
|
|
im.size, self.transform, matrix_down, resample
|
|
|
|
)
|
2022-08-29 19:35:06 +03:00
|
|
|
assert_image_similar(transformed, im, epsilon * epsilon_scale)
|
2022-08-24 15:43:49 +03:00
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
2022-08-29 19:35:06 +03:00
|
|
|
"x, y, epsilon_scale",
|
2022-08-24 15:43:49 +03:00
|
|
|
(
|
|
|
|
(0.1, 0, 3.7),
|
|
|
|
(0.6, 0, 9.1),
|
|
|
|
(50, 50, 0),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
@pytest.mark.parametrize(
|
2022-08-29 19:35:06 +03:00
|
|
|
"resample, epsilon",
|
2022-08-24 15:43:49 +03:00
|
|
|
(
|
2022-01-15 01:02:31 +03:00
|
|
|
(Image.Resampling.NEAREST, 0),
|
2022-08-24 15:43:49 +03:00
|
|
|
(Image.Resampling.BILINEAR, 1.5),
|
2022-01-15 01:02:31 +03:00
|
|
|
(Image.Resampling.BICUBIC, 1),
|
2022-08-24 15:43:49 +03:00
|
|
|
),
|
|
|
|
)
|
2024-02-12 01:28:53 +03:00
|
|
|
def test_translate(
|
|
|
|
self,
|
|
|
|
x: float,
|
|
|
|
y: float,
|
|
|
|
epsilon_scale: float,
|
|
|
|
resample: Image.Resampling,
|
|
|
|
epsilon: float,
|
|
|
|
) -> None:
|
2016-07-10 21:47:59 +03:00
|
|
|
im = self._test_image()
|
2016-07-11 00:47:58 +03:00
|
|
|
|
2016-07-10 21:47:59 +03:00
|
|
|
size_up = int(round(im.width + x)), int(round(im.height + y))
|
2019-06-13 18:54:24 +03:00
|
|
|
matrix_up = [1, 0, -x, 0, 1, -y, 0, 0]
|
|
|
|
matrix_down = [1, 0, x, 0, 1, y, 0, 0]
|
|
|
|
|
2022-08-24 15:43:49 +03:00
|
|
|
transformed = im.transform(size_up, self.transform, matrix_up, resample)
|
|
|
|
transformed = transformed.transform(
|
|
|
|
im.size, self.transform, matrix_down, resample
|
|
|
|
)
|
2022-08-29 19:35:06 +03:00
|
|
|
assert_image_similar(transformed, im, epsilon * epsilon_scale)
|
2016-07-10 16:07:31 +03:00
|
|
|
|
2016-07-10 14:59:36 +03:00
|
|
|
|
2016-07-11 00:26:12 +03:00
|
|
|
class TestImageTransformPerspective(TestImageTransformAffine):
|
2016-08-31 13:12:07 +03:00
|
|
|
# Repeat all tests for AFFINE transformations with PERSPECTIVE
|
2022-01-15 01:02:31 +03:00
|
|
|
transform = Image.Transform.PERSPECTIVE
|