mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-04-06 18:34:14 +03:00
Merge pull request #8805 from radarhere/gimp
Start from zero index when reading GimpPaletteFile
This commit is contained in:
commit
e1bf0f647f
|
@ -32,3 +32,4 @@ def test_get_palette() -> None:
|
|||
|
||||
# Assert
|
||||
assert mode == "RGB"
|
||||
assert len(palette) / 3 == 8
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue
Block a user