Convert subsequent frames of L mode GIF to LA if transparency is present

This commit is contained in:
Andrew Murray 2022-03-01 20:11:35 +11:00
parent 37d28ce514
commit 5c6212052c
3 changed files with 25 additions and 5 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 64 B

View File

@ -59,6 +59,17 @@ def test_invalid_file():
GifImagePlugin.GifImageFile(invalid_file) GifImagePlugin.GifImageFile(invalid_file)
def test_l_mode_transparency():
with Image.open("Tests/images/no_palette_with_transparency.gif") as im:
assert im.mode == "L"
assert im.load()[0, 0] == 0
assert im.info["transparency"] == 255
im.seek(1)
assert im.mode == "LA"
assert im.load()[0, 0] == (0, 255)
def test_optimize(): def test_optimize():
def test_grayscale(optimize): def test_grayscale(optimize):
im = Image.new("L", (1, 1), 0) im = Image.new("L", (1, 1), 0)

View File

@ -167,9 +167,15 @@ class GifImageFile(ImageFile.ImageFile):
if self.__frame == 1: if self.__frame == 1:
self.pyaccess = None self.pyaccess = None
if "transparency" in self.info: if "transparency" in self.info:
self.mode = "RGBA" if self.mode == "P":
self.im.putpalettealpha(self.info["transparency"], 0) self.im.putpalettealpha(self.info["transparency"], 0)
self.im = self.im.convert("RGBA", Image.Dither.FLOYDSTEINBERG) self.im = self.im.convert("RGBA", Image.Dither.FLOYDSTEINBERG)
self.mode = "RGBA"
else:
self.im = self.im.convert_transparent(
"LA", self.info["transparency"]
)
self.mode = "LA"
del self.info["transparency"] del self.info["transparency"]
else: else:
@ -368,8 +374,11 @@ class GifImageFile(ImageFile.ImageFile):
if self.__frame == 0: if self.__frame == 0:
return return
if self._frame_transparency is not None: if self._frame_transparency is not None:
self.im.putpalettealpha(self._frame_transparency, 0) if self.mode == "P":
frame_im = self.im.convert("RGBA") self.im.putpalettealpha(self._frame_transparency, 0)
frame_im = self.im.convert("RGBA")
else:
frame_im = self.im.convert_transparent("LA", self._frame_transparency)
else: else:
frame_im = self.im.convert("RGB") frame_im = self.im.convert("RGB")
frame_im = self._crop(frame_im, self.dispose_extent) frame_im = self._crop(frame_im, self.dispose_extent)