Image.thumbnail() raises ZeroDivisionError when the target height is 0
Maintainers usually reply within 1 day
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
- Domain
- computer-graphics
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
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from python-pillow/Pillow
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
python-pillow/Pillow#10590 ·
Maintainers usually reply within 1 day
-
Sun raster files with RLE encoding and odd-width rows decode shiftedPossibly taken @PerryLink claimed this 21 days ago. Open🤖-assisted
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
python-pillow/Pillow#10370 · 3 comments ·
Maintainers usually reply within 1 day
-
Warn that pre-applying arabic_reshaper + python-bidi corrupts text on a Raqm buildPossibly taken @Syamjith-NK claimed this 85 days ago. OpenDocumentation Font Rendering PNG Third Party Framework
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
python-pillow/Pillow#9907 · 3 comments ·
Maintainers usually reply within 1 day
-
Example code in ImageText documentation makes incorrect use of get_bbox()Possibly taken @mmporong claimed this 1 day ago. OpenDocumentation
Difficulty 1/5 Under an hour Newbie friendliness 25/100
python-pillow/Pillow#10583 · 1 comment ·
Maintainers usually reply within 1 day
-
"ImageEnhance (Color/Contrast/Brightness/Sharpness) .enhance(factor=NaN) silently returns an all-black image"May be free again A pull request for this issue was closed without being merged. OpenAwaiting OP Action
Difficulty 2/5 1-3 hours Newbie friendliness 1/100
python-pillow/Pillow#10567 · 2 comments ·
Maintainers usually reply within 1 day
All issues in python-pillow/Pillow
Similar issues
-
pydanty:is-working
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
pydantic/pydantic-ai#14187 ·
Maintainers usually reply within 1 day
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
Maintainers usually reply within 1 day
-
[Bug]: legacy_json_to_doc crashes on persisted docs with "relationships": nullPossibly taken @Harsh23Kashyap claimed this today. Open
Difficulty 1/5 Under an hour Newbie friendliness 88/100
run-llama/llama_index#24392 ·
Maintainers usually reply within 2 days
-
Difficulty 1/5 Under an hour Newbie friendliness 72/100
pathintegral-institute/mcpm.sh#568 ·
Maintainers usually reply within 21 days