Skip to content

Commit e7fab6a

Browse files
committed
Fixed remapping to palette with duplicate entries
1 parent ad7be55 commit e7fab6a

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,8 @@ def _normalize_palette(im, palette, info):
523523
index = im.palette.colors[source_color]
524524
except KeyError:
525525
index = None
526+
if index in used_palette_colors:
527+
index = None
526528
used_palette_colors.append(index)
527529
for i, index in enumerate(used_palette_colors):
528530
if index is None:

0 commit comments

Comments
 (0)