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

Improve transparency handling when saving GIF images #6176

Merged
merged 3 commits into from May 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
27 changes: 26 additions & 1 deletion Tests/test_file_gif.py
Expand Up @@ -619,7 +619,8 @@ def test_dispose2_background(tmp_path):
assert im.getpixel((0, 0)) == (255, 0, 0)


def test_transparency_in_second_frame():
def test_transparency_in_second_frame(tmp_path):
out = str(tmp_path / "temp.gif")
with Image.open("Tests/images/different_transparency.gif") as im:
assert im.info["transparency"] == 0

Expand All @@ -629,6 +630,14 @@ def test_transparency_in_second_frame():

assert_image_equal_tofile(im, "Tests/images/different_transparency_merged.png")

im.save(out, save_all=True)

with Image.open(out) as reread:
reread.seek(reread.tell() + 1)
assert_image_equal_tofile(
reread, "Tests/images/different_transparency_merged.png"
)


def test_no_transparency_in_second_frame():
with Image.open("Tests/images/iss634.gif") as img:
Expand All @@ -640,6 +649,22 @@ def test_no_transparency_in_second_frame():
assert img.histogram()[255] == 0


def test_remapped_transparency(tmp_path):
out = str(tmp_path / "temp.gif")

im = Image.new("P", (1, 2))
im2 = im.copy()

# Add transparency at a higher index
# so that it will be optimized to a lower index
im.putpixel((0, 1), 5)
im.info["transparency"] = 5
im.save(out, save_all=True, append_images=[im2])

with Image.open(out) as reloaded:
assert reloaded.info["transparency"] == reloaded.getpixel((0, 1))


def test_duration(tmp_path):
duration = 1000

Expand Down
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/GifImagePlugin.py
Expand Up @@ -573,10 +573,14 @@ def _write_multiple_frames(im, fp, palette):
im_frame = _normalize_mode(im_frame.copy())
if frame_count == 0:
for k, v in im_frame.info.items():
if k == "transparency":
continue
im.encoderinfo.setdefault(k, v)
im_frame = _normalize_palette(im_frame, palette, im.encoderinfo)

encoderinfo = im.encoderinfo.copy()
im_frame = _normalize_palette(im_frame, palette, encoderinfo)
if "transparency" in im_frame.info:
encoderinfo.setdefault("transparency", im_frame.info["transparency"])
if isinstance(duration, (list, tuple)):
encoderinfo["duration"] = duration[frame_count]
if isinstance(disposal, (list, tuple)):
Expand Down
7 changes: 7 additions & 0 deletions src/PIL/Image.py
Expand Up @@ -1934,6 +1934,13 @@ def remap_palette(self, dest_map, source_palette=None):
m_im.putpalette(new_palette_bytes)
m_im.palette = ImagePalette.ImagePalette("RGB", palette=palette_bytes)

if "transparency" in self.info:
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

def _get_safe_box(self, size, resample, box):
Expand Down