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

Open Beginner friendly
#10,429 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@Lushiyao5 is already working on this.

Since Nov 5, 2026.

  • #10430 by @Lushiyao5 — open

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
86/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python
Domain
backend

Research direction

Start in PIL/XpmImagePlugin.py at the xpm_head pattern identified in the issue, then find the XPM plugin tests. Reproduce the failure with the supplied values line, and check that the example opens as a 4×4 palette image after whitespace is handled. The reporter offers to submit a PR, but no assignee or linked pull request is listed.

Written by the indexing model from the issue text.

Description

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)
Dominant language
Python
Stars
14k
Forks
2.6k
Avg merge
1d 4h
Merged PRs (30d)
182

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from python-pillow/Pillow

All issues in python-pillow/Pillow

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.