"ImageEnhance (Color/Contrast/Brightness/Sharpness) .enhance(factor=NaN) silently returns an all-black image"

オープン
#10,567 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

  • #10570 @lazerg による — マージされずにクローズ

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
1/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
停滞
技術スタック
python

調査の方向性

Read PIL/ImageEnhance.py at _Enhance.enhance, where all four enhancement classes share the call to Image.blend. Check the existing enhancement tests for the right place to cover a NaN factor across the classes. Done means the shared implementation rejects NaN while preserving the documented behavior for finite factors; pull request #10570 is already open for this issue.

索引モデルが issue の本文から書いたものです。

説明

Awaiting OP Action

enhance() blends the image with its degenerate version using factor;
a NaN factor poisons the blend, and every resulting byte casts back to
uint8 as 0. The result has the exact size and mode of the input - every
downstream check passes - but contains a single value, 0. No exception,
no warning. All four enhancement classes share the behavior (they share the same
base implementation):

import numpy as np
from PIL import Image, ImageEnhance

rs = np.random.RandomState(42)
img = Image.fromarray((np.stack([rs.rand(32, 32), rs.rand(32, 32), rs.rand(32, 32)],
                               axis=-1) * 255).astype(np.uint8))  # RGB, independent channels

for cls in (ImageEnhance.Color, ImageEnhance.Contrast,
            ImageEnhance.Brightness, ImageEnhance.Sharpness):
    out = np.asarray(cls(img).enhance(float("nan")))
    assert out.shape == (32, 32, 3) and np.all(out == 0)
    # uniform 0, unique values = 1, mode/size unchanged;
    # cls(img).enhance(1.0) still returns the original image

(An RGB input keeps the test uniform across all four classes. Side note:
on an L-mode input Color is inherently a no-op - its degenerate IS the
image object, and blending an image with itself returns a copy - so the
L-mode Color path is identity for every factor; the other three have
distinct degenerate images in every mode.)

Root cause (verified, Pillow 13.0.0 source)

PIL/ImageEnhance.py:25-39 - the shared base class:

class _Enhance:
    def enhance(self, factor: float) -> Image.Image:
        ...
        return Image.blend(self.degenerate, self.image, factor)

factor is passed to Image.blend unchecked; blend computes
im1 * (1 - factor) + im2 * factor per pixel, and with factor = NaN
every byte becomes NaN, casting to uint8 as 0. There is no NaN check at
any layer.

Scope note (why NaN only)

Per #7043, the documented contract is that the enhancement factor has
no range limits - large or negative finite factors are legitimate
and their outputs are the expected clamped/saturated blends (e.g.
factor=float("inf") gives a saturated two-value or all-255 result,
Brightness(-inf) clamps to black - consistent with blending toward an
extreme). NaN is not a factor value at all: it silently collapses the
image to a constant that no choice of finite factor produces on typical
inputs.

Impact

Factors are usually computed (new/old with old=0 producing NaN, or
parsed config values); one NaN silently blanks that image, and the
surrounding batch pipeline "succeeds" - shape, mode, and dtype all look
correct.

Suggested fix

Reject NaN in the shared implementation (covers all four classes at
once), leaving finite factors - of any magnitude or sign - untouched:

# PIL/ImageEnhance.py, _Enhance.enhance
if factor != factor:  # NaN check
    raise ValueError("factor must not be NaN")

Environment

  • Pillow 13.0.0, numpy 2.5.5, Python 3.12.16, macOS arm64 (aarch64)

Prior-art check (2026-11-20)

Related: #7043 (enhancement factor range discussion; maintainers state
factors are intentionally unbounded). No report found for the
NaN-factor all-black output. Re-search "ImageEnhance nan" on submission
day.

主要言語
Python
スター
14k
フォーク
2.6k
平均マージ
1日 1時間
マージ済み PR(30日)
205

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

python-pillow/Pillow のほかの issue

python-pillow/Pillow の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。