Skip to content

Commit

Permalink
Fixed loading L mode GIF with transparency
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Feb 24, 2022
1 parent 14b9b59 commit 1c85d44
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
Binary file added Tests/images/no_palette_with_transparency.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions Tests/test_file_gif.py
Expand Up @@ -59,6 +59,17 @@ def test_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 == "RGBA"
assert im.load()[0, 0] == (0, 0, 0, 255)


def test_optimize():
def test_grayscale(optimize):
im = Image.new("L", (1, 1), 0)
Expand Down
16 changes: 12 additions & 4 deletions src/PIL/GifImagePlugin.py
Expand Up @@ -167,9 +167,14 @@ def _seek(self, frame):
if self.__frame == 1:
self.pyaccess = None
if "transparency" in self.info:
if self.mode == "P":
self.im.putpalettealpha(self.info["transparency"], 0)
self.im = self.im.convert("RGBA", Image.Dither.FLOYDSTEINBERG)
else:
self.im = self.im.convert_transparent(
"RGBA", self.info["transparency"]
)
self.mode = "RGBA"
self.im.putpalettealpha(self.info["transparency"], 0)
self.im = self.im.convert("RGBA", Image.Dither.FLOYDSTEINBERG)

del self.info["transparency"]
else:
Expand Down Expand Up @@ -368,8 +373,11 @@ def load_end(self):
if self.__frame == 0:
return
if self._frame_transparency is not None:
self.im.putpalettealpha(self._frame_transparency, 0)
frame_im = self.im.convert("RGBA")
if self.mode == "P":
self.im.putpalettealpha(self._frame_transparency, 0)
frame_im = self.im.convert("RGBA")
else:
frame_im = self.im.convert_transparent("RGBA", self._frame_transparency)
else:
frame_im = self.im.convert("RGB")
frame_im = self._crop(frame_im, self.dispose_extent)
Expand Down

0 comments on commit 1c85d44

Please sign in to comment.