Skip to content

Commit e40033d

Browse files
authored
Merge pull request #8494 from radarhere/gif_palette
Improved handling of RGBA palettes when saving GIF images
2 parents 6e45e71 + 8af2d76 commit e40033d

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

Tests/test_file_gif.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from collections.abc import Generator
55
from io import BytesIO
66
from pathlib import Path
7+
from typing import Any
78

89
import pytest
910

@@ -1435,7 +1436,8 @@ def test_saving_rgba(tmp_path: Path) -> None:
14351436
assert reloaded_rgba.load()[0, 0][3] == 0
14361437

14371438

1438-
def test_optimizing_p_rgba(tmp_path: Path) -> None:
1439+
@pytest.mark.parametrize("params", ({}, {"disposal": 2, "optimize": False}))
1440+
def test_p_rgba(tmp_path: Path, params: dict[str, Any]) -> None:
14391441
out = str(tmp_path / "temp.gif")
14401442

14411443
im1 = Image.new("P", (100, 100))
@@ -1447,7 +1449,7 @@ def test_optimizing_p_rgba(tmp_path: Path) -> None:
14471449
im2 = Image.new("P", (100, 100))
14481450
im2.putpalette(data, "RGBA")
14491451

1450-
im1.save(out, save_all=True, append_images=[im2])
1452+
im1.save(out, save_all=True, append_images=[im2], **params)
14511453

14521454
with Image.open(out) as reloaded:
14531455
assert reloaded.n_frames == 2

src/PIL/GifImagePlugin.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -695,8 +695,9 @@ def _write_multiple_frames(
695695
)
696696
background = _get_background(im_frame, color)
697697
background_im = Image.new("P", im_frame.size, background)
698-
assert im_frames[0].im.palette is not None
699-
background_im.putpalette(im_frames[0].im.palette)
698+
first_palette = im_frames[0].im.palette
699+
assert first_palette is not None
700+
background_im.putpalette(first_palette, first_palette.mode)
700701
bbox = _getbbox(background_im, im_frame)[1]
701702
elif encoderinfo.get("optimize") and im_frame.mode != "1":
702703
if "transparency" not in encoderinfo:

0 commit comments

Comments
 (0)