Use first index for repeated color

This commit is contained in:
Andrew Murray 2021-06-27 16:33:47 +10:00
parent 450382f574
commit 7005e66f00
2 changed files with 12 additions and 4 deletions

View File

@ -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

View File

@ -53,10 +53,12 @@ class ImagePalette:
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()