Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9d1a393

Browse files
authored
Merge pull request #29236 from QuLogic/anim-rgb
ANI: Reduce Pillow frames to RGB when opaque
2 parents 915f672 + 9f0e454 commit 9d1a393

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

lib/matplotlib/animation.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -492,8 +492,15 @@ def grab_frame(self, **savefig_kwargs):
492492
buf = BytesIO()
493493
self.fig.savefig(
494494
buf, **{**savefig_kwargs, "format": "rgba", "dpi": self.dpi})
495-
self._frames.append(Image.frombuffer(
496-
"RGBA", self.frame_size, buf.getbuffer(), "raw", "RGBA", 0, 1))
495+
im = Image.frombuffer(
496+
"RGBA", self.frame_size, buf.getbuffer(), "raw", "RGBA", 0, 1)
497+
if im.getextrema()[3][0] < 255:
498+
# This frame has transparency, so we'll just add it as is.
499+
self._frame.append(im)
500+
else:
501+
# Without transparency, we switch to RGB mode, which converts to P mode a
502+
# little better if needed (specifically, this helps with GIF output.)
503+
self._frames.append(im.convert("RGB"))
497504

498505
def finish(self):
499506
self._frames[0].save(

0 commit comments

Comments
 (0)