Skip to content

Fixed ZeroDivisionError in ImageStat #9105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

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.

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
1 participant