XPM values line with more than one space between numbers raises ValueError
Maintainers usually reply within 1 day
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 86/100
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
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from python-pillow/Pillow
-
Converting F images to I turns large values, inf and NaN into -2147483648Possibly taken @usmanmateen claimed this 5 days ago. OpenConversion
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
python-pillow/Pillow#10372 · 4 comments ·
Maintainers usually reply within 1 day
-
Sun raster files with RLE encoding and odd-width rows decode shiftedPossibly taken @PerryLink claimed this 4 days ago. Open🤖-assisted
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
python-pillow/Pillow#10370 · 3 comments ·
Maintainers usually reply within 1 day
-
Warn that pre-applying arabic_reshaper + python-bidi corrupts text on a Raqm buildPossibly taken @Syamjith-NK claimed this 68 days ago. OpenDocumentation Font Rendering PNG Third Party Framework
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
python-pillow/Pillow#9907 · 2 comments ·
Maintainers usually reply within 1 day
-
Image.putpixel incorrectly labels supported arguments in type annotationsPossibly taken @radarhere claimed this 196 days ago. OpenPNG
Difficulty 2/5 1-3 hours Newbie friendliness 70/100
python-pillow/Pillow#9584 · 1 comment ·
Maintainers usually reply within 1 day
-
`ImageDraw.arc()` segfaults on a tuple subclass whose `__len__` is too largePossibly taken @Parsiffall1 claimed this 7 days ago. Open
Difficulty 4/5 1-2 days Newbie friendliness 58/100
python-pillow/Pillow#10358 · 2 comments ·
Maintainers usually reply within 1 day
All issues in python-pillow/Pillow
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
its-me-prash/vwgroup-connect-ha#2019 ·
Maintainers usually reply within 1 day
-
bug good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
KonstantinStarovoytov/kitden#21 ·
Maintainers usually reply within 1 day
-
RefactorOpen
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
ehmatthes/gh-profiler#166 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
smart-on-fhir/smart-fetch#73 ·
-
good first issue help wanted
Difficulty 1/5 1-3 hours Newbie friendliness 85/100
RizgarOzan/playcal#13 ·