Skip to content

Conversation

@aayushadhikari7
Copy link

Summary

When saving an image with zero width or height, Pillow currently crashes with confusing internal errors:

  • GIF: ValueError: max() iterable argument is empty (in _get_optimize)
  • PNG: SystemError: tile cannot extend outside image

This PR adds early validation in Image.save() to raise a clear error message:

ValueError: cannot save image with zero width or height (size: (0, 50))

Changes

  • src/PIL/Image.py: Added dimension check at start of save() method
  • Tests/test_image.py: Added parametrized test covering (0, 1), (1, 0), and (0, 0) dimensions

Test Plan

  • Added test cases for zero-dimension images (width=0, height=0, both=0)
  • Tests verify both PNG and GIF formats raise the expected error

Fixes #9389

Saving an image with zero width or height now raises a clear ValueError
instead of cryptic internal errors like "max() arg is empty" (GIF) or
"tile cannot extend outside image" (PNG).

Fixes python-pillow#9389
@aayushadhikari7 aayushadhikari7 force-pushed the fix-zero-dimension-save branch from 54c7878 to 26d0088 Compare January 12, 2026 17:19
@aayushadhikari7
Copy link
Author

👍

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.

Confusing errors when saving zero-dimension images instead of clear validation

1 participant