diff --git a/Tests/test_image_resize.py b/Tests/test_image_resize.py index 3ef05a25f..c9e304512 100644 --- a/Tests/test_image_resize.py +++ b/Tests/test_image_resize.py @@ -285,14 +285,14 @@ class TestReducingGapResize: class TestImageResize: def test_resize(self) -> None: - def resize(mode: str, size: tuple[int, int]) -> None: + def resize(mode: str, size: tuple[int, int] | list[int]) -> None: out = hopper(mode).resize(size) assert out.mode == mode - assert out.size == size + assert out.size == tuple(size) for mode in "1", "P", "L", "RGB", "I", "F": resize(mode, (112, 103)) - resize(mode, (188, 214)) + resize(mode, [188, 214]) # Test unknown resampling filter with hopper() as im: diff --git a/Tests/test_numpy.py b/Tests/test_numpy.py index a082e5a4d..312e32e0c 100644 --- a/Tests/test_numpy.py +++ b/Tests/test_numpy.py @@ -198,6 +198,15 @@ def test_putdata() -> None: assert len(im.getdata()) == len(arr) +def test_resize() -> None: + im = hopper() + size = (64, 64) + + im_resized = im.resize(numpy.array(size)) + + assert im_resized.size == size + + @pytest.mark.parametrize( "dtype", ( diff --git a/src/PIL/Image.py b/src/PIL/Image.py index c9bb008b0..dbb72ed6a 100644 --- a/src/PIL/Image.py +++ b/src/PIL/Image.py @@ -63,7 +63,6 @@ from . import ( ) from ._binary import i32le, o32be, o32le from ._deprecate import deprecate -from ._typing import StrOrBytesPath, TypeGuard from ._util import DeferredError, is_path ElementTree: ModuleType | None @@ -220,6 +219,7 @@ if hasattr(core, "DEFAULT_STRATEGY"): if TYPE_CHECKING: from . import ImageFile, ImagePalette + from ._typing import NumpyArray, StrOrBytesPath, TypeGuard ID: list[str] = [] OPEN: dict[ str, @@ -2203,7 +2203,7 @@ class Image: def resize( self, - size: tuple[int, int], + size: tuple[int, int] | list[int] | NumpyArray, resample: int | None = None, box: tuple[float, float, float, float] | None = None, reducing_gap: float | None = None, @@ -2211,7 +2211,7 @@ class Image: """ Returns a resized copy of this image. - :param size: The requested size in pixels, as a 2-tuple: + :param size: The requested size in pixels, as a tuple or array: (width, height). :param resample: An optional resampling filter. This can be one of :py:data:`Resampling.NEAREST`, :py:data:`Resampling.BOX`, @@ -2276,6 +2276,7 @@ class Image: if box is None: box = (0, 0) + self.size + size = tuple(size) if self.size == size and box == (0, 0) + self.size: return self.copy()