Skip to content

Prevent negative ImageStat variance - #9994

Merged
radarhere merged 5 commits into
python-pillow:mainfrom
lllleolin-max:fix/stat-variance
Sep 14, 2026
Merged

Prevent negative ImageStat variance#9994
radarhere merged 5 commits into
python-pillow:mainfrom
lllleolin-max:fix/stat-variance

Conversation

@lllleolin-max

@lllleolin-max lllleolin-max commented Sep 13, 2026

Copy link
Copy Markdown
Contributor

On affected platforms, a constant white image of size (919, 405) can produce a slightly negative ImageStat.Stat.var due to floating-point rounding, causing stddev to raise a math domain error.

Current implementation (updated 2026-09-14):

Adopted @radarhere's suggestions from lllleolin-max#2, preserving all three original commits through e5203ab.

  • Accumulate sum and sum2 as integers and use integer exponentiation, avoiding the rounding in this regression instead of clamping the result afterwards. Their return annotations are now list[int]; variance remains floating-point.
  • Keep the regression focused on the affected constant image and assert var == [0.0].
  • Simplify the release note accordingly.

Validation of this revision:

  • 12 related ImageStat, ImageEnhance and histogram tests passed, with no skips.
  • 781 temporary numerical checks passed, covering constant images, masks, RGB, histograms and nonzero-variance controls.
  • selftest.py: 59 passed.
  • Black, Ruff, mypy, Sphinx-lint and whitespace checks passed.

Local validation used Windows/Python 3.12.11 with this checkout's ImageStat and ImageEnhance modules and Pillow 12.3.0 prebuilt native extensions and remaining modules. Native extensions were not rebuilt; the full test suite was not run locally. This does not claim all floating-point variance rounding is eliminated. New upstream CI is pending completion.

Prepared with AI assistance; the adopted implementation and test simplification are @radarhere's original commits.

@radarhere radarhere added the 🤖-assisted AI-assisted label Sep 13, 2026
@radarhere

Copy link
Copy Markdown
Member

I've found that the floating point error can be fixed by using integers. See lllleolin-max#2

@radarhere radarhere changed the title Prevent negative ImageStat variance from floating-point rounding Prevent negative ImageStat variance Sep 14, 2026
@radarhere
radarhere merged commit 3413abf into python-pillow:main Sep 14, 2026
53 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🤖-assisted AI-assisted

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants