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

Allow size argument to resize() to be a NumPy array #8201

Merged
merged 2 commits into from
Jul 5, 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
6 changes: 3 additions & 3 deletions Tests/test_image_resize.py
Original file line number Diff line number Diff line change
Expand Up @@ -285,14 +285,14 @@ def test_box_filter(

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:
Expand Down
9 changes: 9 additions & 0 deletions Tests/test_numpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
(
Expand Down
7 changes: 4 additions & 3 deletions src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
)
from ._binary import i32le, o32be, o32le
from ._deprecate import deprecate
from ._typing import StrOrBytesPath, TypeGuard
from ._util import DeferredError, is_path

ElementTree: ModuleType | None
Expand Down Expand Up @@ -220,6 +219,7 @@ class Quantize(IntEnum):

if TYPE_CHECKING:
from . import ImageFile, ImagePalette
from ._typing import NumpyArray, StrOrBytesPath, TypeGuard
ID: list[str] = []
OPEN: dict[
str,
Expand Down Expand Up @@ -2203,15 +2203,15 @@ def _get_safe_box(self, size, resample, box):

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,
) -> 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`,
Expand Down Expand Up @@ -2276,6 +2276,7 @@ def resize(
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()

Expand Down
Loading