ImageOps.autocontrast() raises IndexError for a one-element cutoff tuple

オープン 初心者向け
#10,590 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

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

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

評価

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

調査の方向性

バグは src/PIL/ImageOps.py の 141〜161 行付近にある autocontrast にあります。ここでは長さを確認せずにタプルの cutoff を使っているため、要素が 1 つのタプルだと cutoff[1] が失敗します。issue のスニペットで再現してから、既存のテスト test_autocontrast_invalid_ignore の検証スタイルを確認してください。cutoff のタプルが正確に 2 要素でない場合に、わかりやすいメッセージとともに ValueError を送出し、その動作を新しいテストで確認できれば完了です。

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

説明

What did you do?
from PIL import Image, ImageOps

im = Image.new("L", (4, 4), 128)
ImageOps.autocontrast(im, cutoff=(10,))
What did you expect to happen?

A ValueError saying the cutoff tuple must have two elements, matching
how the same function validates its other argument: a bad ignore value
raises ValueError: ignore must be between or equal to 0 and 255
(tested in test_autocontrast_invalid_ignore). The documented shape is
"Either a tuple of (low, high), or a single number for both."

What actually happened?
Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
  File ".../PIL/ImageOps.py", line 161, in autocontrast
    cut = int(n * cutoff[1] // 100)
IndexError: tuple index out of range
What are your OS, Python and Pillow versions?
  • OS: Ubuntu 24.04
  • Python: 3.12.3
  • Pillow: 13.1.0.dev0 (main, commit 688d01d)
Evidence

src/PIL/ImageOps.py, autocontrast, lines 141-161:

if cutoff:
    # cut off pixels from both ends of the histogram
    if not isinstance(cutoff, tuple):
        cutoff = (cutoff, cutoff)
    # get number of pixels
    n = sum(h)
    # remove cutoff% pixels from the low end
    cut = int(n * cutoff[0] // 100)
    ...
    # remove cutoff% samples from the high end
    cut = int(n * cutoff[1] // 100)

A non-tuple cutoff is normalised to a 2-tuple, but a tuple is used as-is
with no length check, so a 1-tuple passes the if cutoff: test and then
cutoff[1] raises IndexError. A 3-tuple is silently accepted and the
extra element ignored.

主要言語
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 を短くまとめたダイジェスト。