Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions Tests/test_file_gif.py
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,51 @@ def test_remapped_transparency(tmp_path: Path) -> None:
assert reloaded.info["transparency"] == reloaded.getpixel((0, 1))


@pytest.mark.parametrize(
"transparency, expected",
(
(b"\xff\x00\x80\x00", 1),
(b"\xff\x80", None),
),
)
def test_bytes_transparency(
tmp_path: Path, transparency: bytes, expected: int | None
) -> None:
out = tmp_path / "temp.gif"

# Alpha values for each palette entry, as read from a PNG image
im = Image.new("P", (4, 1))
im.putpalette((255, 0, 0, 0, 255, 0, 0, 0, 255, 255, 255, 255))
for x in range(4):
im.putpixel((x, 0), x)
expected_im = im.convert("RGB")
im.info["transparency"] = transparency
im.save(out)

assert im.info["transparency"] == transparency
with Image.open(out) as reloaded:
assert reloaded.info.get("transparency") == expected
assert_image_equal(reloaded.convert("RGB"), expected_im)


@pytest.mark.parametrize("disposal", (0, 2))
def test_bytes_transparency_save_all(tmp_path: Path, disposal: int) -> None:
out = tmp_path / "temp.gif"

im = Image.new("P", (4, 1))
im.putpalette((255, 0, 0, 0, 255, 0, 0, 0, 255, 255, 255, 255))
for x in range(4):
im.putpixel((x, 0), x)
im.info["transparency"] = b"\xff\x00\x80\x00"
im2 = im.transpose(Image.Transpose.FLIP_LEFT_RIGHT)
im.save(out, save_all=True, append_images=[im2], disposal=disposal)

with Image.open(out) as reloaded:
assert isinstance(reloaded, GifImagePlugin.GifImageFile)
assert reloaded.n_frames == 2
assert reloaded.info["transparency"] == 1


def test_duration(tmp_path: Path) -> None:
duration = 1000

Expand Down
21 changes: 19 additions & 2 deletions src/PIL/GifImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,16 @@ def _normalize_mode(im: Image.Image) -> Image.Image:
_Palette = bytes | bytearray | list[int] | ImagePalette.ImagePalette


def _get_transparency(transparency: int | bytes | None) -> int | None:
if isinstance(transparency, bytes):
# Alpha values for each palette entry, as read from a PNG image.
# GIF only has a single transparent index,
# so use the first fully transparent entry
index = transparency.find(0)
return index if index != -1 else None
return transparency


def _normalize_palette(
im: Image.Image, palette: _Palette | None, info: dict[str, Any]
) -> Image.Image:
Expand All @@ -551,6 +561,13 @@ def _normalize_palette(
:param info: encoderinfo
:returns: Image object
"""
if isinstance(info.get("transparency"), bytes):
transparency = _get_transparency(info["transparency"])
if transparency is None:
del info["transparency"]
else:
info["transparency"] = transparency

source_palette = None
if palette:
# a bytes palette
Expand Down Expand Up @@ -701,8 +718,8 @@ def _write_multiple_frames(
if im_frames[-1].encoderinfo.get("disposal") == 2:
# To appear correctly in viewers using a convention,
# only consider transparency, and not background color
color = im.encoderinfo.get(
"transparency", im.info.get("transparency")
color = _get_transparency(
im.encoderinfo.get("transparency", im.info.get("transparency"))
)
if color is not None:
if background_im is None:
Expand Down