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

Skip to content

Commit fc44d8a

Browse files
committed
Cleanup Animation frame_formats.
- Warn if the requested frame format is unsupported and fallback occurs. - FFMpegFileWriter does not support 'raw' or 'rgba' (AFAICT there's no way to forward the relevant frame size/bitdepth info). - 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), but it does not support 'raw' (per https://imagemagick.org/script/formats.php). 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 fc44d8a

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

lib/matplotlib/animation.py

Lines changed: 16 additions & 12 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 (
@@ -296,8 +297,11 @@ def __init__(self, fps=5, codec=None, bitrate=None, extra_args=None,
296297

297298
super().__init__(fps=fps, metadata=metadata, codec=codec,
298299
bitrate=bitrate)
299-
300-
self.frame_format = 'rgba'
300+
# The rcParam only applies to FileMovieWriters, but should be read here
301+
# because setting to frame_format to 'rgba' would otherwise spuriously
302+
# warn that that format is unsupported e.g. for FFMpegFileWriter.
303+
self.frame_format = (mpl.rcParams['animation.frame_format']
304+
if isinstance(self, FileMovieWriter) else 'rgba')
301305
self.extra_args = extra_args
302306

303307
def _adjust_frame_size(self):
@@ -395,9 +399,6 @@ class FileMovieWriter(MovieWriter):
395399
396400
This must be sub-classed to be useful.
397401
"""
398-
def __init__(self, *args, **kwargs):
399-
super().__init__(*args, **kwargs)
400-
self.frame_format = mpl.rcParams['animation.frame_format']
401402

402403
@cbook._delete_parameter("3.3", "clear_temp")
403404
def setup(self, fig, outfile, dpi=None, frame_prefix=None,
@@ -467,6 +468,10 @@ def frame_format(self, frame_format):
467468
if frame_format in self.supported_formats:
468469
self._frame_format = frame_format
469470
else:
471+
cbook._warn_external(
472+
f"Ignoring file format {frame_format!r} which is not "
473+
f"supported by {type(self).__name__}; using "
474+
f"{self.supported_formats[0]} instead.")
470475
self._frame_format = self.supported_formats[0]
471476

472477
def _base_temp_name(self):
@@ -529,7 +534,6 @@ def setup(self, fig, outfile, dpi=None):
529534
self._frames = []
530535

531536
def grab_frame(self, **savefig_kwargs):
532-
from PIL import Image
533537
buf = BytesIO()
534538
self.fig.savefig(
535539
buf, **{**savefig_kwargs, "format": "rgba", "dpi": self.dpi})
@@ -629,8 +633,7 @@ class FFMpegFileWriter(FFMpegBase, FileMovieWriter):
629633
Frames are written to temporary files on disk and then stitched
630634
together at the end.
631635
"""
632-
supported_formats = ['png', 'jpeg', 'ppm', 'tiff', 'sgi', 'bmp',
633-
'pbm', 'raw', 'rgba']
636+
supported_formats = ['png', 'jpeg', 'ppm', 'tiff', 'sgi', 'bmp', 'pbm']
634637

635638
def _args(self):
636639
# Returns the command line parameters for subprocess to use
@@ -744,14 +747,15 @@ class ImageMagickFileWriter(ImageMagickBase, FileMovieWriter):
744747
745748
Frames are written to temporary files on disk and then stitched
746749
together at the end.
747-
748750
"""
749751

750-
supported_formats = ['png', 'jpeg', 'ppm', 'tiff', 'sgi', 'bmp',
751-
'pbm', 'raw', 'rgba']
752+
supported_formats = ['png', 'jpeg', 'ppm', 'tiff', 'sgi', 'bmp', 'pbm',
753+
'rgba']
752754

753755
def _args(self):
754-
return ([self.bin_path(), '-delay', str(self.delay), '-loop', '0',
756+
return ([self.bin_path(),
757+
'-size', '%ix%i' % self.frame_size, '-depth', '8',
758+
'-delay', str(self.delay), '-loop', '0',
755759
'%s*.%s' % (self.temp_prefix, self.frame_format)]
756760
+ self.output_args)
757761

0 commit comments

Comments
 (0)