Skip to content

Commit

Permalink
Merge pull request #6548 from radarhere/gif_palette
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Sep 21, 2022
2 parents f73499e + 841ba4a commit 920bcec
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
13 changes: 13 additions & 0 deletions Tests/test_file_gif.py
Expand Up @@ -1087,6 +1087,19 @@ def test_palette_save_P(tmp_path):
assert_image_equal(reloaded, im)


def test_palette_save_duplicate_entries(tmp_path):
im = Image.new("P", (1, 2))
im.putpixel((0, 1), 1)

im.putpalette((0, 0, 0, 0, 0, 0))

out = str(tmp_path / "temp.gif")
im.save(out, palette=[0, 0, 0, 0, 0, 0, 1, 1, 1])

with Image.open(out) as reloaded:
assert reloaded.convert("RGB").getpixel((0, 1)) == (0, 0, 0)


def test_palette_save_all_P(tmp_path):
frames = []
colors = ((255, 0, 0), (0, 255, 0))
Expand Down
5 changes: 2 additions & 3 deletions src/PIL/GifImagePlugin.py
Expand Up @@ -519,9 +519,8 @@ def _normalize_palette(im, palette, info):
used_palette_colors = []
for i in range(0, len(source_palette), 3):
source_color = tuple(source_palette[i : i + 3])
try:
index = im.palette.colors[source_color]
except KeyError:
index = im.palette.colors.get(source_color)
if index in used_palette_colors:
index = None
used_palette_colors.append(index)
for i, index in enumerate(used_palette_colors):
Expand Down

0 comments on commit 920bcec

Please sign in to comment.