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

クローズ 初心者向け
#10,429 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

関連するプルリクエストがすでにマージされています。

  • #10430 @Lushiyao5 による — マージ済み

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
86/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python
領域
backend

調査の方向性

issueで特定されたパターン xpm_head の箇所から PIL/XpmImagePlugin.py を読み始め、次にXPMプラグインのテストを探してください。提示されたvalues行で失敗を再現し、空白を処理した後に例が4×4のパレット画像として開くことを確認してください。報告者はPRの提出を申し出ていますが、担当者もリンクされたプルリクエストも記載されていません。

索引モデルが 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日 3時間
マージ済み PR(30日)
185

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

python-pillow/Pillow のほかの issue

python-pillow/Pillow の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。