Skip to content

Commit

Permalink
Remove transparency if it cannot be remapped
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed May 21, 2022
1 parent 46a80d1 commit 99f4623
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
14 changes: 14 additions & 0 deletions Tests/test_image.py
Expand Up @@ -607,6 +607,20 @@ def test_remap_palette(self):
with pytest.raises(ValueError):
im.remap_palette(None)

def test_remap_palette_transparency(self):
im = Image.new("P", (1, 2))
im.putpixel((0, 1), 1)
im.info["transparency"] = 0

im_remapped = im.remap_palette([1, 0])
assert im_remapped.info["transparency"] == 1

# Test unused transparency
im.info["transparency"] = 2

im_remapped = im.remap_palette([1, 0])
assert "transparency" not in im_remapped.info

def test__new(self):
im = hopper("RGB")
im_p = hopper("P")
Expand Down
6 changes: 5 additions & 1 deletion src/PIL/Image.py
Expand Up @@ -1935,7 +1935,11 @@ def remap_palette(self, dest_map, source_palette=None):
m_im.palette = ImagePalette.ImagePalette("RGB", palette=palette_bytes)

if "transparency" in self.info:
m_im.info["transparency"] = new_positions[self.info["transparency"]]
try:
m_im.info["transparency"] = dest_map.index(self.info["transparency"])
except ValueError:
if "transparency" in m_im.info:
del m_im.info["transparency"]

return m_im

Expand Down

0 comments on commit 99f4623

Please sign in to comment.