XPM values line with more than one space between numbers raises ValueError

Aperta Adatta ai principianti
#10,429 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@Lushiyao5 ci sta già lavorando.

Dal 5/11/2026.

  • #10430 di @Lushiyao5 — aperta

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
86/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
backend

Direzione di ricerca

Inizia da PIL/XpmImagePlugin.py, dal pattern xpm_head identificato nell’issue, poi trova i test del plugin XPM. Riproduci il problema con la riga values fornita e verifica che l’esempio si apra come immagine a palette 4×4 dopo aver gestito gli spazi bianchi. Chi ha segnalato il problema si offre di inviare una PR, ma non è indicato alcun assegnatario né alcuna pull request collegata.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

What did you do?

Opened an XPM file whose values line has more than one space between the numbers. GROMACS writes its matrix output this way (for example the RMSD matrix from gmx rms -m), and we ran into it in Galaxy when reading metadata from these files.

from io import BytesIO

from PIL import Image

data = b"""/* XPM */
static char *gromacs_xpm[] = {
"4 4   2 1",
"A  c #FFFFFF ",
"B  c #000000 ",
"ABAB",
"BABA",
"ABAB",
"BABA"
};
"""
with Image.open(BytesIO(data)) as im:
    print(im.size, im.mode)
What did you expect to happen?

(4, 4) P

libXpm reads the values with xpmNextUI/xpmNextWord (src/data.c), which skips any run of whitespace before each number. ImageMagick also reads the file (XPM 4x4 8-bit sRGB 2c).

What actually happened?
  File ".../PIL/XpmImagePlugin.py", line 59, in _open
    palette_length = int(m.group(3))
ValueError: invalid literal for int() with base 10: b''

xpm_head matches exactly one space between the numbers:

xpm_head = re.compile(b'"([0-9]*) ([0-9]*) ([0-9]*) ([0-9]*)')

so with "4 4 2 1" the third group matches the empty string. With \s+ in place of the single spaces, the example opens as (4, 4) P and loads.

I'm happy to open a PR with that change and a test, if that sounds right to you.

What are your OS, Python and Pillow versions?
  • OS: macOS 26.5.2 (arm64)
  • Python: 3.14.2
  • Pillow: 13.0.0 (also 12.3.0; the regex is unchanged on main)
Lingua principale
Python
Stelle
14k
Fork
2.6k
Merge medio
1g 4h
PR unite (30g)
182

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di python-pillow/Pillow

Tutte le issue di python-pillow/Pillow

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.