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

Abierto Apto para principiantes
#10,429 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

@Lushiyao5 ya está trabajando en esto.

Desde el 5/11/2026.

  • #10430 de @Lushiyao5 — abierto

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
86/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
python
Área
backend

Línea de trabajo

Empieza en PIL/XpmImagePlugin.py, en el patrón xpm_head identificado en la issue, y luego busca las pruebas del plugin XPM. Reproduce el fallo con la línea values proporcionada y comprueba que el ejemplo se abre como una imagen de paleta de 4×4 después de procesar los espacios en blanco. La persona que informa del problema se ofrece a enviar un PR, pero no consta ninguna persona asignada ni ninguna pull request vinculada.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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)
Lenguaje dominante
Python
Estrellas
14k
Forks
2.6k
Merge medio
1 d 4 h
PR fusionados (30 d)
182

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de python-pillow/Pillow

Todos los issues de python-pillow/Pillow

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.