Skip to content

Commit 1ed6039

Browse files
authored
Merge pull request #1 from radarhere/gimppalette
Only use colors from the palette file
2 parents 681f77a + 4642e02 commit 1ed6039

File tree

2 files changed

+16
-34
lines changed

2 files changed

+16
-34
lines changed

Tests/test_file_gimppalette.py

+6-23
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,8 @@ def test_get_palette():
2929
palette, mode = palette_file.getpalette()
3030

3131
# Assert
32-
assert mode == "RGB"
33-
34-
35-
def test_palette__has_correct_color_indexes():
36-
# Arrange
37-
with open("Tests/images/custom_gimp_palette.gpl", "rb") as fp:
38-
palette_file = GimpPaletteFile(fp)
39-
40-
palette, mode = palette_file.getpalette()
41-
42-
colors_in_test_palette = [
32+
expected_palette = b""
33+
for color in (
4334
(0, 0, 0),
4435
(65, 38, 30),
4536
(103, 62, 49),
@@ -48,15 +39,7 @@ def test_palette__has_correct_color_indexes():
4839
(208, 127, 100),
4940
(151, 144, 142),
5041
(221, 207, 199),
51-
]
52-
53-
for i, color in enumerate(colors_in_test_palette):
54-
assert tuple(palette[i * 3 : i * 3 + 3]) == color
55-
56-
57-
def test_palette_counts_number_of_colors_in_file():
58-
# Arrange
59-
with open("Tests/images/custom_gimp_palette.gpl", "rb") as fp:
60-
palette_file = GimpPaletteFile(fp)
61-
62-
assert palette_file.n_colors == 8
42+
):
43+
expected_palette += bytes(color)
44+
assert palette == expected_palette
45+
assert mode == "RGB"

src/PIL/GimpPaletteFile.py

+10-11
Original file line numberDiff line numberDiff line change
@@ -26,28 +26,27 @@ class GimpPaletteFile:
2626

2727
def __init__(self, fp):
2828

29-
palette = bytearray(b"".join([o8(i) * 3 for i in range(256)]))
30-
3129
if fp.readline()[:12] != b"GIMP Palette":
3230
raise SyntaxError("not a GIMP palette file")
3331

34-
index = 0
35-
for s in fp:
32+
self.palette = b""
33+
while len(self.palette) < 768:
34+
35+
s = fp.readline()
36+
if not s:
37+
break
38+
3639
# skip fields and comment lines
3740
if re.match(rb"\w+:|#", s):
3841
continue
3942
if len(s) > 100:
4043
raise SyntaxError("bad palette file")
4144

42-
v = tuple(map(int, s.split()[:3]))
45+
v = s.split()
4346
if len(v) < 3:
4447
raise ValueError("bad palette entry")
45-
46-
palette[index * 3 : index * 3 + 3] = v
47-
index += 1
48-
49-
self.palette = bytes(palette)
50-
self.n_colors = index
48+
for i in range(3):
49+
self.palette += o8(int(v[i]))
5150

5251
def getpalette(self):
5352

0 commit comments

Comments
 (0)