mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-08-03 03:50:09 +03:00
Fix indices of colors read from GIMP Palette file
This commit is contained in:
parent
285f9cbb12
commit
61b1eeb39f
|
@ -2,11 +2,11 @@ GIMP Palette
|
|||
Name: custompalette
|
||||
Columns: 4
|
||||
#
|
||||
0 0 0 Index 3
|
||||
65 38 30 Index 4
|
||||
103 62 49 Index 6
|
||||
79 73 72 Index 7
|
||||
114 101 97 Index 8
|
||||
208 127 100 Index 9
|
||||
151 144 142 Index 10
|
||||
221 207 199 Index 11
|
||||
0 0 0 Index 0
|
||||
65 38 30 Index 1
|
||||
103 62 49 Index 2
|
||||
79 73 72 Index 3
|
||||
114 101 97 Index 4
|
||||
208 127 100 Index 5
|
||||
151 144 142 Index 6
|
||||
221 207 199 Index 7
|
||||
|
|
|
@ -30,3 +30,33 @@ def test_get_palette():
|
|||
|
||||
# Assert
|
||||
assert mode == "RGB"
|
||||
|
||||
|
||||
def test_palette__has_correct_color_indexes():
|
||||
# Arrange
|
||||
with open("Tests/images/custom_gimp_palette.gpl", "rb") as fp:
|
||||
palette_file = GimpPaletteFile(fp)
|
||||
|
||||
palette, mode = palette_file.getpalette()
|
||||
|
||||
colors_in_test_palette = [
|
||||
(0, 0, 0),
|
||||
(65, 38, 30),
|
||||
(103, 62, 49),
|
||||
(79, 73, 72),
|
||||
(114, 101, 97),
|
||||
(208, 127, 100),
|
||||
(151, 144, 142),
|
||||
(221, 207, 199),
|
||||
]
|
||||
|
||||
for i, color in enumerate(colors_in_test_palette):
|
||||
assert tuple(palette[i * 3: i * 3 + 3]) == color
|
||||
|
||||
|
||||
def test_palette_counts_number_of_colors_in_file():
|
||||
# Arrange
|
||||
with open("Tests/images/custom_gimp_palette.gpl", "rb") as fp:
|
||||
palette_file = GimpPaletteFile(fp)
|
||||
|
||||
assert palette_file.n_colors == 8
|
||||
|
|
|
@ -18,6 +18,8 @@ import re
|
|||
|
||||
from ._binary import o8
|
||||
|
||||
_str_to_o8 = lambda v: o8(int(v))
|
||||
|
||||
|
||||
class GimpPaletteFile:
|
||||
"""File handler for GIMP's palette format."""
|
||||
|
@ -26,17 +28,13 @@ class GimpPaletteFile:
|
|||
|
||||
def __init__(self, fp):
|
||||
|
||||
self.palette = [o8(i) * 3 for i in range(256)]
|
||||
palette = bytearray(b"".join([o8(i) * 3 for i in range(256)]))
|
||||
|
||||
if fp.readline()[:12] != b"GIMP Palette":
|
||||
raise SyntaxError("not a GIMP palette file")
|
||||
|
||||
for i in range(256):
|
||||
|
||||
s = fp.readline()
|
||||
if not s:
|
||||
break
|
||||
|
||||
index = 0
|
||||
for s in fp:
|
||||
# skip fields and comment lines
|
||||
if re.match(rb"\w+:|#", s):
|
||||
continue
|
||||
|
@ -44,12 +42,15 @@ class GimpPaletteFile:
|
|||
raise SyntaxError("bad palette file")
|
||||
|
||||
v = tuple(map(int, s.split()[:3]))
|
||||
if len(v) != 3:
|
||||
if len(v) < 3:
|
||||
raise ValueError("bad palette entry")
|
||||
|
||||
self.palette[i] = o8(v[0]) + o8(v[1]) + o8(v[2])
|
||||
palette[index * 3: index * 3 + 3] = v
|
||||
index += 1
|
||||
|
||||
self.palette = bytes(palette)
|
||||
self.n_colors = index
|
||||
|
||||
self.palette = b"".join(self.palette)
|
||||
|
||||
def getpalette(self):
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user