8000 FIX: pre-composite animation frames to white background · matplotlib/matplotlib@faf45b4 · GitHub
[go: up one dir, main page]

Skip to content

Commit faf45b4

Browse files
committed
FIX: pre-composite animation frames to white background
This fixes issues with encoding that do not support transparency and when the Figure facecolor is transparent. Closes #19040
1 parent a4dbe0d commit faf45b4

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

lib/matplotlib/animation.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
from matplotlib._animation_data import (
3939
DISPLAY_TEMPLATE, INCLUDED_FRAMES, JS_INCLUDE, STYLE_INCLUDE)
4040
from matplotlib import _api, cbook
41-
41+
import matplotlib.colors as mcolors
4242

4343
_log = logging.getLogger(__name__)
4444

@@ -1012,6 +1012,9 @@ def func(current_frame: int, total_frames: int) -> Any
10121012

10131013
if savefig_kwargs is None:
10141014
savefig_kwargs = {}
1015+
else:
1016+
# we are going to mutate this below
1017+
savefig_kwargs = dict(savefig_kwargs)
10151018

10161019
if fps is None and hasattr(self, '_interval'):
10171020
# Convert interval in ms to frames per second
@@ -1067,6 +1070,18 @@ def func(current_frame: int, total_frames: int) -> Any
10671070
_log.info("Disabling savefig.bbox = 'tight', as it may cause "
10681071
"frame size to vary, which is inappropriate for "
10691072
"animation.")
1073+
1074+
facecolor = savefig_kwargs.get('facecolor',
1075+
mpl.rcParams['savefig.facecolor'])
1076+
if facecolor == 'auto':
1077+
facecolor = self._fig.get_facecolor()
1078+
1079+
def _pre_composite_to_white(color):
1080+
r, g, b, a = mcolors.to_rgba(color)
1081+
return a * np.array([r, g, b]) + 1 - a
1082+
1083+
savefig_kwargs['facecolor'] = _pre_composite_to_white(facecolor)
1084+
savefig_kwargs['transparent'] = False # just to be safe!
10701085
# canvas._is_saving = True makes the draw_event animation-starting
10711086
# callback a no-op; canvas.manager = None prevents resizing the GUI
10721087
# widget (both are likewise done in savefig()).

0 commit comments

Comments
 (0)
0