Skip to content

ZeroDivisionError in ImageStat #9104

@mvanderlee

Description

@mvanderlee

What did you do?

Called ImageState.Stat.rms and for some images, we get a ZeroDivisionError. We do not have access to the images that triggered it and were not able to reproduce yet.

What did you expect to happen?

I expect Pillow to validate before dividing by zero.

What actually happened?

An error was thrown

What are your OS, Python and Pillow versions?

  • OS: Windows 11, and Ubuntu
  • Python: 3.10.15
  • Pillow: 10.4.0
  File "/usr/local/lib/python3.10/functools.py", line 981, in __get__
    val = self.func(instance)
  File "/usr/local/lib/python3.10/site-packages/PIL/ImageStat.py", line 144, in rms
    return [math.sqrt(self.sum2[i] / self.count[i]) for i in self.bands]
  File "/usr/local/lib/python3.10/site-packages/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
    diff = ImageChops.difference(img1, img2)
    stat = ImageStat.Stat(diff, mask=binary_mask)
    rms = sum([v**2 for v in stat.rms]) ** 0.5

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions