Skip to content

Commit 920bcec

Browse files
authored
Merge pull request #6548 from radarhere/gif_palette
2 parents f73499e + 841ba4a commit 920bcec

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

Tests/test_file_gif.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,6 +1087,19 @@ def test_palette_save_P(tmp_path):
10871087
assert_image_equal(reloaded, im)
10881088

10891089

1090+
def test_palette_save_duplicate_entries(tmp_path):
1091+
im = Image.new("P", (1, 2))
1092+
im.putpixel((0, 1), 1)
1093+
1094+
im.putpalette((0, 0, 0, 0, 0, 0))
1095+
1096+
out = str(tmp_path / "temp.gif")
1097+
im.save(out, palette=[0, 0, 0, 0, 0, 0, 1, 1, 1])
1098+
1099+
with Image.open(out) as reloaded:
1100+
assert reloaded.convert("RGB").getpixel((0, 1)) == (0, 0, 0)
1101+
1102+
10901103
def test_palette_save_all_P(tmp_path):
10911104
frames = []
10921105
colors = ((255, 0, 0), (0, 255, 0))

src/PIL/GifImagePlugin.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -519,9 +519,8 @@ def _normalize_palette(im, palette, info):
519519
used_palette_colors = []
520520
for i in range(0, len(source_palette), 3):
521521
source_color = tuple(source_palette[i : i + 3])
522-
try:
523-
index = im.palette.colors[source_color]
524-
except KeyError:
522+
index = im.palette.colors.get(source_color)
523+
if index in used_palette_colors:
525524
index = None
526525
used_palette_colors.append(index)
527526
for i, index in enumerate(used_palette_colors):

0 commit comments

Comments
 (0)