Skip to content

ZeroDivisionError in ImageStat #9104

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

Activity

  1. radarhere commented on Jul 22, 2025

    @radarhere
    Member

    Hi. As you will have gathered from the traceback, self.count[i] is zero.

    count is the https://pillow.readthedocs.io/en/stable/reference/ImageStat.html#PIL.ImageStat.Stat.count

    Total number of pixels for each band in the image.

    The problem can be replicated quite simply by generating an image that is zero width by zero height.

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

    I expect Pillow to validate before dividing by zero.

    Are you saying that Pillow should throw a custom error when ImageStat.Stat() is called with a zero by zero image? I would have thought a neater option would just be to return zero values as the rms.

    Also, you have said that you are using Pillow 10.4.0. I don't know what reason you have for not upgrading to the latest version, but be aware that if you wanted any fix we make for this, you would have to upgrade to at least Pillow 12.0.0, as that is the next release we have planned.

  2. radarhere commented on Jul 23, 2025

    @radarhere
    Member

    I've created #9105 to return zero values.

    If you do find the image that triggered this, please let us know.

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

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