Image.thumbnail() raises ZeroDivisionError when the target height is 0

Open Beginner friendly
#10,589 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@vladosskins178 is already working on this.

Since Nov 22, 2026.

  • #10591 by @vladosskins178 — open

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
70/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Active
Tech stack
python

Research direction

The crash is in Image.thumbnail in src/PIL/Image.py, in the nested preserve_aspect_ratio helper, where x / y runs after the self.height == 0 guard but with no guard for a zero target height. Reproduce with thumbnail((5, 0)) and compare with thumbnail((0, 5)), which already returns a (0, 0) image. Done means no ZeroDivisionError, a behavior consistent with the zero-width case, and a regression test added beside the existing zero-dimension tests in Tests/test_image_thumbnail.py. The expected result is not settled in the issue, so confirm it with a maintainer first.

Written by the indexing model from the issue text.

Description

What did you do?
from PIL import Image

im = Image.new("L", (10, 10), 128)
im.thumbnail((0, 0))

The same happens for any target size with a zero height, for example
thumbnail((5, 0)) and thumbnail((100, 0)).

What did you expect to happen?

A clean error, or a zero-size result consistent with the rest of
thumbnail. Image.resize((0, 0)) raises
ValueError: height and width must be > 0, and thumbnail itself already
handles zero dimensions elsewhere: thumbnail((0, 5)) returns a (0, 0)
image, and the test suite covers zero-size images
(test_division_by_zero, test_zero_dimension in
Tests/test_image_thumbnail.py).

What actually happened?
Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
  File ".../PIL/Image.py", line 2964, in thumbnail
    if x / y >= aspect:
ZeroDivisionError: division by zero
What are your OS, Python and Pillow versions?
  • OS: Ubuntu 24.04
  • Python: 3.12.3
  • Pillow: 13.1.0.dev0 (main, commit 688d01d)
Evidence

src/PIL/Image.py, Image.thumbnail, lines 2950-2964 (inside the nested
preserve_aspect_ratio):

provided_size = tuple(map(math.floor, size))
...
x, y = provided_size
if x >= self.width and y >= self.height:
    return None
if self.height == 0:
    return x, self.height

aspect = self.width / self.height
if x / y >= aspect:

There is a guard for a zero-height image (self.height == 0), but no
guard for a zero-height target size (y == 0), so x / y divides by
zero. The zero-width target case works: thumbnail((0, 5)) returns a
(0, 0) image.

Dominant language
Python
Stars
14k
Forks
2.6k
Avg merge
1d 1h
Merged PRs (30d)
205

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from python-pillow/Pillow

All issues in python-pillow/Pillow

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.