Converting F images to I turns large values, inf and NaN into -2147483648
I maintainer di solito rispondono entro 1 giorno
Valutazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Idoneità per principianti
- 72/100
Direzione di ricerca
Il bug è il cast non controllato i = f; nella funzione f2i in src/libImaging/Convert.c; clip32() in src/libImaging/Filter.c mostra la verifica dell'intervallo già presente da seguire. Riproduci con lo snippet Python della issue, poi conferma i risultati da F a I. È fatto quando i valori sopra l'intervallo di INT32 vengono limitati a 2147483647, -inf viene limitato a -2147483648, NaN diventa 0 e il round trip restituisce 2147483647.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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
- Lingua principale
- Python
- Stelle
- 14k
- Fork
- 2.6k
- Merge medio
- 1g 5h
- PR unite (30g)
- 179
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di python-pillow/Pillow
-
Sun raster files with RLE encoding and odd-width rows decode shiftedForse già presa @PerryLink l’ha presa 2 giorni fa. Aperta🤖-assisted
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
python-pillow/Pillow#10370 · 3 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
Warn that pre-applying arabic_reshaper + python-bidi corrupts text on a Raqm buildForse già presa @Syamjith-NK l’ha presa 65 giorni fa. ApertaDocumentation Font Rendering PNG Third Party Framework
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
python-pillow/Pillow#9907 · 2 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
Image.putpixel incorrectly labels supported arguments in type annotationsForse già presa @radarhere l’ha presa 193 giorni fa. ApertaPNG
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
python-pillow/Pillow#9584 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
`ImageDraw.arc()` segfaults on a tuple subclass whose `__len__` is too largeForse già presa @Parsiffall1 l’ha presa 4 giorni fa. Aperta
Difficoltà 4/5 1-2 giorni Idoneità per principianti 58/100
python-pillow/Pillow#10358 · 2 commenti ·
I maintainer di solito rispondono entro 1 giorno
-
JPEG saved with keep_rgb=True and dpi reopens with incorrect colorsForse già presa @Muguett-DBY l’ha presa 6 giorni fa. Aperta🤖-assisted JPEG
Difficoltà 4/5 1-2 giorni Idoneità per principianti 25/100
python-pillow/Pillow#10344 ·
I maintainer di solito rispondono entro 1 giorno
Tutte le issue di python-pillow/Pillow
Issue simili
-
Import errors are swallowed: PerthImplicitWatermarker silently becomes NoneForse già presa @JonRC l’ha presa oggi. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
resemble-ai/Perth#23 ·
-
correction metadata
Difficoltà 2/5 Meno di un'ora Idoneità per principianti 65/100
acl-org/acl-anthology#10753 · 1 commento ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
earthtojake/text-to-cad#758 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 1/5 1-3 ore Idoneità per principianti 70/100
DataTalksClub/faq#545 ·
I maintainer di solito rispondono entro 2 giorni
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
I maintainer di solito rispondono entro 1 giorno