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

未关闭 适合新手
#10,429 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@Lushiyao5 已经在做这个了。

开始于 2026年11月5日。

  • #10430 来自 @Lushiyao5 —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
86/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
python
领域
backend

调研方向

从 issue 中确定的 xpm_head 模式处开始查看 PIL/XpmImagePlugin.py,然后找到 XPM 插件的测试。使用提供的 values 行重现故障,并检查处理空白后示例是否能作为 4×4 调色板图像打开。报告者愿意提交 PR,但未列出负责人或关联的 pull request。

由索引模型根据 Issue 内容生成。

描述

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)
主要语言
Python
星标
14k
派生
2.6k
平均合并
1 天 4 小时
30 天内合并 PR
182

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

python-pillow/Pillow 的其他 Issue

查看 python-pillow/Pillow 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。