Skip to content

Commit 0850ba0

Browse files
committed
Cleanup Animation frame_formats.
- Warn if the requested frame format is unsupported and fallback occurs. - ImageMagickFileWriter supports 'rgba' but requires explicit frame_size and depth to do so (they are the same as for ImageMagickWriter, and passing them doesn't negatively impact other formats); it does not support 'raw' (per https://imagemagick.org/script/formats.php) but we can just override it as 'rgba'. Test script: ```python import numpy as np import matplotlib.pyplot as plt import matplotlib.animation as animation fig, ax = plt.subplots() x = np.arange(0, 2*np.pi, 0.01) line, = ax.plot(x, np.sin(x)) def animate(i): line.set_ydata(np.sin(x + i / 50)) # update the data. return line, ani = animation.FuncAnimation( fig, animate, interval=50, blit=True, save_count=20) plt.rcParams["animation.frame_format"] = "rgba" # or "raw" writer = animation.FFMpegFileWriter() # or animation.ImageMagickFileWriter() ani.save("/tmp/movie.mp4", writer=writer) ``` (Also move import of PIL up, as it's a standard dependency now.)
1 parent 7c813db commit 0850ba0

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

lib/matplotlib/animation.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import warnings
3232

3333
import numpy as np
34+
from PIL import Image
3435

3536
import matplotlib as mpl
3637
from matplotlib._animation_data import (
@@ -263,6 +264,10 @@ class MovieWriter(AbstractMovieWriter):
263264
exec_key = cbook._deprecate_privatize_attribute("3.3")
264265
args_key = cbook._deprecate_privatize_attribute("3.3")
265266

267+
# Pipe-based writers only support RGBA, but file-based ones support more
268+
# formats.
269+
supported_formats = ["rgba"]
270+
266271
def __init__(self, fps=5, codec=None, bitrate=None, extra_args=None,
267272
metadata=None):
268273
"""
@@ -296,8 +301,7 @@ def __init__(self, fps=5, codec=None, bitrate=None, extra_args=None,
296301

297302
super().__init__(fps=fps, metadata=metadata, codec=codec,
298303
bitrate=bitrate)
299-
300-
self.frame_format = 'rgba'
304+
self.frame_format = self.supported_formats[0]
301305
self.extra_args = extra_args
302306

303307
def _adjust_frame_size(self):
@@ -467,6 +471,10 @@ def frame_format(self, frame_format):
467471
if frame_format in self.supported_formats:
468472
self._frame_format = frame_format
469473
else:
474+
cbook._warn_external(
475+
f"Ignoring file format {frame_format!r} which is not "
476+
f"supported by {type(self).__name__}; using "
477+
f"{self.supported_formats[0]} instead.")
470478
self._frame_format = self.supported_formats[0]
471479

472480
def _base_temp_name(self):
@@ -529,7 +537,6 @@ def setup(self, fig, outfile, dpi=None):
529537
self._frames = []
530538

531539
def grab_frame(self, **savefig_kwargs):
532-
from PIL import Image
533540
buf = BytesIO()
534541
self.fig.savefig(
535542
buf, **{**savefig_kwargs, "format": "rgba", "dpi": self.dpi})
@@ -744,15 +751,18 @@ class ImageMagickFileWriter(ImageMagickBase, FileMovieWriter):
744751
745752
Frames are written to temporary files on disk and then stitched
746753
together at the end.
747-
748754
"""
749755

750756
supported_formats = ['png', 'jpeg', 'ppm', 'tiff', 'sgi', 'bmp',
751757
'pbm', 'raw', 'rgba']
752758

753759
def _args(self):
754-
return ([self.bin_path(), '-delay', str(self.delay), '-loop', '0',
755-
'%s*.%s' % (self.temp_prefix, self.frame_format)]
760+
# Force format: ImageMagick does not recognize 'raw'.
761+
fmt = 'rgba:' if self.frame_format == 'raw' else ''
762+
return ([self.bin_path(),
763+
'-size', '%ix%i' % self.frame_size, '-depth', '8',
764+
'-delay', str(self.delay), '-loop', '0',
765+
'%s%s*.%s' % (fmt, self.temp_prefix, self.frame_format)]
756766
+ self.output_args)
757767

758768

0 commit comments

Comments
 (0)