8000 FIX: pre-composite animation frames to white background by tacaswell · Pull Request #21831 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

FIX: pre-composite animation frames to white background #21831

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

Merged
merged 2 commits into from
Nov 23, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion lib/matplotlib/animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
from matplotlib._animation_data import (
DISPLAY_TEMPLATE, INCLUDED_FRAMES, JS_INCLUDE, STYLE_INCLUDE)
from matplotlib import _api, cbook

import matplotlib.colors as mcolors

_log = logging.getLogger(__name__)

Expand Down Expand Up @@ -1002,6 +1002,9 @@ def func(current_frame: int, total_frames: int) -> Any

if savefig_kwargs is None:
savefig_kwargs = {}
else:
# we are going to mutate this below
savefig_kwargs = dict(savefig_kwargs)

if fps is None and hasattr(self, '_interval'):
# Convert interval in ms to frames per second
Expand Down Expand Up @@ -1057,6 +1060,18 @@ def func(current_frame: int, total_frames: int) -> Any
_log.info("Disabling savefig.bbox = 'tight', as it may cause "
"frame size to vary, which is inappropriate for "
"animation.")

facecolor = savefig_kwargs.get('facecolor',
mpl.rcParams['savefig.facecolor'])
if facecolor == 'auto':
facecolor = self._fig.get_facecolor()

def _pre_composite_to_white(color):
r, g, b, a = mcolors.to_rgba(color)
return a * np.array([r, g, b]) + 1 - a

savefig_kwargs['facecolor'] = _pre_composite_to_white(facecolor)
savefig_kwargs['transparent'] = False # just to be safe!
# canvas._is_saving = True makes the draw_event animation-starting
# callback a no-op; canvas.manager = None prevents resizing the GUI
# widget (both are likewise done in savefig()).
Expand Down
6 changes: 5 additions & 1 deletion lib/matplotlib/tests/test_animation.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ def finish(self):

def test_null_movie_writer(anim):
# Test running an animation with NullMovieWriter.
plt.rcParams["savefig.facecolor"] = "auto"
filename = "unused.null"
dpi = 50
savefig_kwargs = dict(foo=0)
Expand All @@ -82,7 +83,10 @@ def test_null_movie_writer(anim):
assert writer.outfile == filename
assert writer.dpi == dpi
assert writer.args == ()
assert writer.savefig_kwargs == savefig_kwargs
# we enrich the savefig kwargs to ensure we composite transparent
# output to an opaque background
for k, v in savefig_kwargs.items():
assert writer.savefig_kwargs[k] == v
assert writer._count == anim.save_count


Expand Down
0