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
16 changes: 16 additions & 0 deletions Tests/test_imageops.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,22 @@ def test_contain_round() -> None:
assert new_im.height == 5


def test_contain_narrow_image() -> None:
im = Image.new("RGB", (100, 1), "red")
new_im = ImageOps.contain(im, (10, 10))
assert new_im.size == (10, 1)

im = Image.new("RGB", (1, 100), "red")
new_im = ImageOps.contain(im, (10, 10))
assert new_im.size == (1, 10)


def test_pad_narrow_image() -> None:
im = Image.new("RGB", (100, 1), "red")
new_im = ImageOps.pad(im, (10, 10))
assert new_im.size == (10, 10)


@pytest.mark.parametrize(
"image_name, expected_size",
(
Expand Down
7 changes: 7 additions & 0 deletions docs/releasenotes/13.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,10 @@ position or contents.

:py:meth:`~PIL.Image.Image.tobytes` also now explicitly cleans up its encoder on
both success and failure, rather than relying on object destruction.

Fixed ImageOps.contain() for very thin images
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

:py:func:`~PIL.ImageOps.contain` and :py:func:`~PIL.ImageOps.pad` no longer raise
``ValueError`` when a very thin image rounds the shorter side to zero pixels. The
calculated dimension is now kept at least one pixel.
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