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

Use transparency behind first GIF frame and when disposing to background #5557

Merged
merged 4 commits into from
Jun 27, 2021
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
Binary file added Tests/images/first_frame_transparency.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Tests/images/transparent_dispose.gif
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 16 additions & 0 deletions Tests/test_file_gif.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,12 @@ def test_eoferror():
im.seek(n_frames - 1)


def test_first_frame_transparency():
with Image.open("Tests/images/first_frame_transparency.gif") as im:
px = im.load()
assert px[0, 0] == im.info["transparency"]


def test_dispose_none():
with Image.open("Tests/images/dispose_none.gif") as img:
try:
Expand Down Expand Up @@ -331,6 +337,16 @@ def test_dispose_background():
pass


def test_transparent_dispose():
expected_colors = [(2, 1, 2), (0, 1, 0), (2, 1, 2)]
with Image.open("Tests/images/transparent_dispose.gif") as img:
for frame in range(3):
img.seek(frame)
for x in range(3):
color = img.getpixel((x, 0))
assert color == expected_colors[frame][x]


def test_dispose_previous():
with Image.open("Tests/images/dispose_prev.gif") as img:
try:
Expand Down
15 changes: 13 additions & 2 deletions src/PIL/GifImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,14 @@ def _seek(self, frame):
dispose_size = (x1 - x0, y1 - y0)

Image._decompression_bomb_check(dispose_size)
self.dispose = Image.core.fill(
"P", dispose_size, self.info.get("background", 0)

# by convention, attempt to use transparency first
color = (
frame_transparency
if frame_transparency is not None
else self.info.get("background", 0)
)
self.dispose = Image.core.fill("P", dispose_size, color)
else:
# replace with previous contents
if self.im:
Expand Down Expand Up @@ -317,6 +322,12 @@ def _seek(self, frame):
if self.palette:
self.mode = "P"

def load_prepare(self):
if not self.im and "transparency" in self.info:
self.im = Image.core.fill(self.mode, self.size, self.info["transparency"])

super(GifImageFile, self).load_prepare()

def tell(self):
return self.__frame

Expand Down