Skip to content

Commit

Permalink
Use first index for repeated color
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Jun 27, 2021
1 parent 450382f commit 7005e66
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
6 changes: 6 additions & 0 deletions Tests/test_image_convert.py
Expand Up @@ -155,6 +155,12 @@ def test_trns_RGB(tmp_path):
assert "transparency" not in im_p.info
im_p.save(f)

im = Image.new("RGB", (1, 1))
im.info["transparency"] = im.getpixel((0, 0))
im_p = im.convert("P", palette=Image.ADAPTIVE)
assert im_p.info["transparency"] == im_p.getpixel((0, 0))
im_p.save(f)


def test_gif_with_rgba_palette_to_p():
# See https://github.com/python-pillow/Pillow/issues/2433
Expand Down
10 changes: 6 additions & 4 deletions src/PIL/ImagePalette.py
Expand Up @@ -53,10 +53,12 @@ def palette(self, palette):
self._palette = palette

mode_len = len(self.mode)
self.colors = {
tuple(self.palette[i : i + mode_len]): i // mode_len
for i in range(0, len(self.palette), mode_len)
}
self.colors = {}
for i in range(0, len(self.palette), mode_len):
color = tuple(self.palette[i : i + mode_len])
if color in self.colors:
continue
self.colors[color] = i // mode_len

def copy(self):
new = ImagePalette()
Expand Down

0 comments on commit 7005e66

Please sign in to comment.