Skip to content
Closed
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
14 changes: 14 additions & 0 deletions Tests/test_imageops.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,20 @@ def test_contain(new_size: tuple[int, int]) -> None:
assert new_im.size == (256, 256)


@pytest.mark.parametrize("source_size", ((100, 1), (1, 100), (20, 1), (1, 20)))
def test_contain_minimum_dimension(source_size: tuple[int, int]) -> None:
im = Image.new("RGB", source_size, "red")
result = ImageOps.contain(im, (10, 10))
expected_size = (10, 1) if source_size[0] > source_size[1] else (1, 10)
assert result.size == expected_size
assert result.getpixel((0, 0)) == (255, 0, 0)

padded = ImageOps.pad(im, (10, 10), color="blue")
assert padded.size == (10, 10)
assert padded.getpixel((4, 4)) == (255, 0, 0)
assert padded.getpixel((0, 0)) == (0, 0, 255)


def test_contain_round() -> None:
im = Image.new("1", (43, 63), 1)
new_im = ImageOps.contain(im, (5, 7))
Expand Down
8 changes: 8 additions & 0 deletions docs/releasenotes/13.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ Two new filters are available for :py:meth:`~PIL.Image.Image.resize` and
Other changes
=============

Keep resized dimensions positive in ImageOps.contain()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

:py:func:`~PIL.ImageOps.contain` now keeps the calculated width or height at a
minimum of one pixel. Previously, resizing a very narrow image could round one
dimension to zero and raise ``ValueError``. This also fixes
:py:func:`~PIL.ImageOps.pad` for these images.

Python 3.15
^^^^^^^^^^^

Expand Down
4 changes: 2 additions & 2 deletions src/PIL/ImageOps.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,11 +300,11 @@ def contain(

if im_ratio != dest_ratio:
if im_ratio > dest_ratio:
new_height = round(image.height / image.width * size[0])
new_height = max(1, round(image.height / image.width * size[0]))
if new_height != size[1]:
size = (size[0], new_height)
else:
new_width = round(image.width / image.height * size[1])
new_width = max(1, round(image.width / image.height * size[1]))
if new_width != size[0]:
size = (new_width, size[1])
return image.resize(size, resample=method)
Expand Down
Loading