Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only use colors from the palette file #1

Merged
merged 1 commit into from
Oct 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 6 additions & 23 deletions Tests/test_file_gimppalette.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,8 @@ def test_get_palette():
palette, mode = palette_file.getpalette()

# 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 = [
expected_palette = b""
for color in (
(0, 0, 0),
(65, 38, 30),
(103, 62, 49),
Expand All @@ -48,15 +39,7 @@ def test_palette__has_correct_color_indexes():
(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
):
expected_palette += bytes(color)
assert palette == expected_palette
assert mode == "RGB"
21 changes: 10 additions & 11 deletions src/PIL/GimpPaletteFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,27 @@ class GimpPaletteFile:

def __init__(self, fp):

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")

index = 0
for s in fp:
self.palette = b""
while len(self.palette) < 768:

s = fp.readline()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, GIMP GPL files are not limited to 256 colors - a file with more than 256 colors is a valid resource: althoug indexed images can only use this many colors, the resources in the app can also function as color source for various painting tools and filters, which does not have this limitation.

Reading to the file end makes more sense - and later on we adjust the higher level ImagePalette to deal (or error) with larger palettes.

if not s:
break

# skip fields and comment lines
if re.match(rb"\w+:|#", s):
continue
if len(s) > 100:
raise SyntaxError("bad palette file")

v = tuple(map(int, s.split()[:3]))
v = s.split()
if len(v) < 3:
raise ValueError("bad palette entry")

palette[index * 3 : index * 3 + 3] = v
index += 1

self.palette = bytes(palette)
self.n_colors = index
for i in range(3):
self.palette += o8(int(v[i]))

def getpalette(self):

Expand Down