Skip to content

Allow ImageOps.contain() to create zero dimension images#9673

Open
radarhere wants to merge 1 commit into
python-pillow:mainfrom
radarhere:zero
Open

Allow ImageOps.contain() to create zero dimension images#9673
radarhere wants to merge 1 commit into
python-pillow:mainfrom
radarhere:zero

Conversation

@radarhere

@radarhere radarhere commented Jun 14, 2026

Copy link
Copy Markdown
Member

ImageOps.contain() attempts to fits an image within a certain size. If those calculations end up with an image where one dimension is zero, then an error is currently raised.

>>> from PIL import Image, ImageOps
>>> im = Image.new("1", (1, 10))
>>> ImageOps.contain(im, (5, 5))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "PIL/ImageOps.py", line 300, in contain
    return image.resize(size, resample=method)
  File "PIL/Image.py", line 2430, in resize
    im = self.im.resize(size, resample, box)
ValueError: height and width must be > 0

This PR suggests allowing zero dimension images to be created. It is an alternative to #9672, which forces the image to be at least 1px wide or high.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant