Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit bdf178e

Browse filesBrowse files
authored
Merge pull request #21831 from tacaswell/fix_transparent_animation
FIX: pre-composite animation frames to white background
2 parents 7413bf0 + a4e3cf3 commit bdf178e
Copy full SHA for bdf178e

File tree

Expand file treeCollapse file tree

2 files changed

+21
-2
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+21
-2
lines changed

‎lib/matplotlib/animation.py

Copy file name to clipboardExpand all lines: lib/matplotlib/animation.py
+16-1Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
from matplotlib._animation_data import (
4040
DISPLAY_TEMPLATE, INCLUDED_FRAMES, JS_INCLUDE, STYLE_INCLUDE)
4141
from matplotlib import _api, cbook
42-
42+
import matplotlib.colors as mcolors
4343

4444
_log = logging.getLogger(__name__)
4545

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

10041004
if savefig_kwargs is None:
10051005
savefig_kwargs = {}
1006+
else:
1007+
# we are going to mutate this below
1008+
savefig_kwargs = dict(savefig_kwargs)
10061009

10071010
if fps is None and hasattr(self, '_interval'):
10081011
# Convert interval in ms to frames per second
@@ -1057,6 +1060,18 @@ def func(current_frame: int, total_frames: int) -> Any
10571060
_log.info("Disabling savefig.bbox = 'tight', as it may cause "
10581061
"frame size to vary, which is inappropriate for "
10591062
"animation.")
1063+
1064+
facecolor = savefig_kwargs.get('facecolor',
1065+
mpl.rcParams['savefig.facecolor'])
1066+
if facecolor == 'auto':
1067+
facecolor = self._fig.get_facecolor()
1068+
1069+
def _pre_composite_to_white(color):
1070+
r, g, b, a = mcolors.to_rgba(color)
1071+
return a * np.array([r, g, b]) + 1 - a
1072+
1073+
savefig_kwargs['facecolor'] = _pre_composite_to_white(facecolor)
1074+
savefig_kwargs['transparent'] = False # just to be safe!
10601075
# canvas._is_saving = True makes the draw_event animation-starting
10611076
# callback a no-op; canvas.manager = None prevents resizing the GUI
10621077
# widget (both are likewise done in savefig()).

‎lib/matplotlib/tests/test_animation.py

Copy file name to clipboardExpand all lines: lib/matplotlib/tests/test_animation.py
+5-1Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ def finish(self):
7070

7171
def test_null_movie_writer(anim):
7272
# Test running an animation with NullMovieWriter.
73+
plt.rcParams["savefig.facecolor"] = "auto"
7374
filename = "unused.null"
7475
dpi = 50
7576
savefig_kwargs = dict(foo=0)
@@ -82,7 +83,10 @@ def test_null_movie_writer(anim):
8283
assert writer.outfile == filename
8384
assert writer.dpi == dpi
8485
assert writer.args == ()
85-
assert writer.savefig_kwargs == savefig_kwargs
86+
# we enrich the savefig kwargs to ensure we composite transparent
87+
# output to an opaque background
88+
for k, v in savefig_kwargs.items():
89+
assert writer.savefig_kwargs[k] == v
8690
assert writer._count == anim.save_count
8791

8892

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.