Skip to content

Commit

Permalink
Backport PR matplotlib#29236: ANI: Reduce Pillow frames to RGB when o…
Browse files Browse the repository at this point in the history
…paque
  • Loading branch information
timhoffm authored and meeseeksmachine committed Dec 5, 2024
1 parent 4a18447 commit ddbd08e
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions lib/matplotlib/animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -492,8 +492,15 @@ def grab_frame(self, **savefig_kwargs):
buf = BytesIO()
self.fig.savefig(
buf, **{**savefig_kwargs, "format": "rgba", "dpi": self.dpi})
self._frames.append(Image.frombuffer(
"RGBA", self.frame_size, buf.getbuffer(), "raw", "RGBA", 0, 1))
im = Image.frombuffer(
"RGBA", self.frame_size, buf.getbuffer(), "raw", "RGBA", 0, 1)
if im.getextrema()[3][0] < 255:
# This frame has transparency, so we'll just add it as is.
self._frame.append(im)
else:
# Without transparency, we switch to RGB mode, which converts to P mode a
# little better if needed (specifically, this helps with GIF output.)
self._frames.append(im.convert("RGB"))

def finish(self):
self._frames[0].save(
Expand Down

0 comments on commit ddbd08e

Please sign in to comment.