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

Fixed GIF remapping to palette with duplicate entries #6548

Merged
merged 2 commits into from Sep 21, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
2 changes: 2 additions & 0 deletions src/PIL/GifImagePlugin.py
Expand Up @@ -523,6 +523,8 @@ def _normalize_palette(im, palette, info):
index = im.palette.colors[source_color]
except KeyError:
index = None
if index in used_palette_colors:
index = None
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the if could be inside the try, since if there's an exception it will have already been set to None.

Copy link
Member Author

Choose a reason for hiding this comment

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

I actually have a different idea of how to simplify the code. In my latest commit, I replace the try block with get().

used_palette_colors.append(index)
for i, index in enumerate(used_palette_colors):
if index is None:
Expand Down