Skip to content

Conversation

@radarhere
Copy link
Member

Resolves #9104

If an image has zero dimensions, then ImageStat may raise a ZeroDivisionError.

>>> from PIL import Image, ImageStat
>>> im = Image.new("RGB", (0, 0))
>>> stat = ImageStat.Stat(im)
>>> stat.count
[0, 0, 0]
>>> stat.rms
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "functools.py", line 993, in __get__
    val = self.func(instance)
  File "PIL/ImageStat.py", line 144, in rms
    return [math.sqrt(self.sum2[i] / self.count[i]) for i in self.bands]
  File "PIL/ImageStat.py", line 144, in <listcomp>
    return [math.sqrt(self.sum2[i] / self.count[i]) for i in self.bands]
ZeroDivisionError: float division by zero

This just returns zero values for this scenario instead.

@hugovk hugovk changed the title Fixed ZeroDivisionError in ImageStat Fix ZeroDivisionError in ImageStat Aug 27, 2025
@hugovk hugovk merged commit 146d03c into python-pillow:main Aug 27, 2025
73 of 74 checks passed
@radarhere radarhere deleted the imagestat branch August 27, 2025 20:07
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.

ZeroDivisionError in ImageStat

2 participants