Fixed GIF remapping to palette with duplicate entries#6548
Merged
hugovk merged 2 commits intopython-pillow:mainfrom Sep 21, 2022
Merged
Fixed GIF remapping to palette with duplicate entries#6548hugovk merged 2 commits intopython-pillow:mainfrom
hugovk merged 2 commits intopython-pillow:mainfrom
Conversation
Yay295
reviewed
Aug 29, 2022
Comment on lines
523
to
+527
| index = im.palette.colors[source_color] | ||
| except KeyError: | ||
| index = None | ||
| if index in used_palette_colors: | ||
| index = None |
Contributor
There was a problem hiding this comment.
I think the if could be inside the try, since if there's an exception it will have already been set to None.
Member
Author
There was a problem hiding this comment.
I actually have a different idea of how to simplify the code. In my latest commit, I replace the try block with get().
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Addresses #6389
remap_palettecannot map multiple palette indexes to the same output index, by its very definition. It accepts a list of palette indexes that it transforms to the position in the list. There cannot be more than one palette index at a given position in the list.If a palette is supplied when saving a GIF image,
im.save("out.gif", palette=...), and that palette has duplicate entries, then the following GifImagePlugin code will try to prepare a list that maps the first of the duplicated entries to multiple locations, leaving the rest of the duplicate entries to be not strictly defined in the behaviour ofremap_palette.Pillow/src/PIL/GifImagePlugin.py
Lines 519 to 526 in ad7be55
This PR fixes that, by leaving the duplicated indexes as they are - setting
indextoNone, the next loop in GifImagePluginPillow/src/PIL/GifImagePlugin.py
Lines 527 to 532 in ad7be55
will tell
remap_paletteto map the index to itself, leaving it untouched.