Merge pull request #8805 from radarhere/gimp

Start from zero index when reading GimpPaletteFile
This commit is contained in:
Hugo van Kemenade 2025-03-18 15:32:24 +02:00 committed by GitHub
commit e1bf0f647f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 7 additions and 9 deletions

View File

@ -32,3 +32,4 @@ def test_get_palette() -> None:
# Assert
assert mode == "RGB"
assert len(palette) / 3 == 8

View File

@ -18,8 +18,6 @@ from __future__ import annotations
import re
from typing import IO
from ._binary import o8
class GimpPaletteFile:
"""File handler for GIMP's palette format."""
@ -27,13 +25,12 @@ class GimpPaletteFile:
rawmode = "RGB"
def __init__(self, fp: IO[bytes]) -> None:
palette = [o8(i) * 3 for i in range(256)]
if not fp.readline().startswith(b"GIMP Palette"):
msg = "not a GIMP palette file"
raise SyntaxError(msg)
for i in range(256):
palette: list[int] = []
for _ in range(256):
s = fp.readline()
if not s:
break
@ -45,14 +42,14 @@ class GimpPaletteFile:
msg = "bad palette file"
raise SyntaxError(msg)
v = tuple(map(int, s.split()[:3]))
if len(v) != 3:
v = s.split(maxsplit=3)
if len(v) < 3:
msg = "bad palette entry"
raise ValueError(msg)
palette[i] = o8(v[0]) + o8(v[1]) + o8(v[2])
palette += (int(v[i]) for i in range(3))
self.palette = b"".join(palette)
self.palette = bytes(palette)
def getpalette(self) -> tuple[bytes, str]:
return self.palette, self.rawmode