Converting F images to I turns large values, inf and NaN into -2147483648
メンテナーはふだん 1 日以内に返信
評価
調査の方向性
バグは、範囲チェックのないキャスト i = f; で、場所は f2i 関数(src/libImaging/Convert.c)です。clip32()(src/libImaging/Filter.c)に既存の範囲ガードがあるので、それに倣ってください。issue の Python スニペットで再現し、F から I への結果を確認してください。INT32 の範囲を超える値は 2147483647 にクリップされ、-inf は -2147483648 にクリップされ、NaN は 0 になり、ラウンドトリップで 2147483647 が返れば完了です。
索引モデルが issue の本文から書いたものです。
説明
What did you do?
Converted a float (F) image with large or non-finite values to I, for example a float TIFF or a NumPy result with inf or NaN pixels, or an I image that went through F for processing:
import numpy as np
from PIL import Image
im = Image.fromarray(np.array([[1.5, 70000, 3e9, np.inf, -np.inf, np.nan]], np.float32), "F")
print("I", np.asarray(im.convert("I")).tolist())
print("L", np.asarray(im.convert("L")).tolist())
print(Image.new("I", (1, 1), 2**31 - 1).convert("F").convert("I").getpixel((0, 0)))
What did you expect to happen?
Values outside the I range clipped to it, the way F to L and F to I;16 clip to theirs, and the round trip to keep 2147483647:
I [[1, 70000, 2147483647, 2147483647, -2147483648, 0]]
L [[1, 255, 255, 255, 0, 0]]
2147483647
What actually happened?
Every value above the INT32 range, inf and NaN become -2147483648, the most negative value:
I [[1, 70000, -2147483648, -2147483648, -2147483648, -2147483648]]
L [[1, 255, 255, 255, 0, 0]]
-2147483648
The round trip fails because 2147483647 isn't representable as a float32 and rounds up to 2147483648.0, which is just past the INT32 range.
f2i in Convert.c casts with i = f; and no range check. Converting an out-of-range float or NaN to an integer is undefined behavior in C (-fsanitize=float-cast-overflow reports it), and the result depends on the CPU: x86 gives INT_MIN, while ARM saturates and gives INT32_MAX for large values and 0 for NaN. clip32() in Filter.c already guards the same overflow to INT_MIN.
I can open a PR that clips to the INT32 range in f2i and maps NaN to 0.
What are your OS, Python and Pillow versions?
- OS: Ubuntu 26.04 (WSL2), x86-64
- Python: 3.12.13
- Pillow: 13.0.0
- 主要言語
- Python
- スター
- 14k
- フォーク
- 2.6k
- 平均マージ
- 1日 4時間
- マージ済み PR(30日)
- 182
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
python-pillow/Pillow のほかの issue
-
XPM values line with more than one space between numbers raises ValueError対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 86/100
python-pillow/Pillow#10429 ·
メンテナーはふだん 1 日以内に返信
-
Sun raster files with RLE encoding and odd-width rows decode shifted対応中かも @PerryLink が 4 日前に担当しました。 オープン🤖-assisted
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
python-pillow/Pillow#10370 · コメント 3 件 ·
メンテナーはふだん 1 日以内に返信
-
Warn that pre-applying arabic_reshaper + python-bidi corrupts text on a Raqm build対応中かも @Syamjith-NK が 68 日前に担当しました。 オープンDocumentation Font Rendering PNG Third Party Framework
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
python-pillow/Pillow#9907 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
Image.putpixel incorrectly labels supported arguments in type annotations対応中かも @radarhere が 196 日前に担当しました。 オープンPNG
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
python-pillow/Pillow#9584 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
`ImageDraw.arc()` segfaults on a tuple subclass whose `__len__` is too large対応中かも @Parsiffall1 が 7 日前に担当しました。 オープン
難易度 4/5 1〜2日 初心者へのやさしさ 58/100
python-pillow/Pillow#10358 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
python-pillow/Pillow の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 64/100
its-me-prash/vwgroup-connect-ha#2019 ·
メンテナーはふだん 1 日以内に返信
-
bug good first issue
難易度 2/5 1〜3時間 初心者へのやさしさ 86/100
KonstantinStarovoytov/kitden#21 ·
メンテナーはふだん 1 日以内に返信
-
Refactorオープン
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
ehmatthes/gh-profiler#166 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
smart-on-fhir/smart-fetch#73 ·
-
good first issue help wanted
難易度 1/5 1〜3時間 初心者へのやさしさ 85/100
RizgarOzan/playcal#13 ·