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

Skip to content

Commit 73be90b

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 73be90b

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

lib/matplotlib/animation.py

Lines changed: 11 additions & 5 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,7 +297,6 @@ 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-
300300
self.frame_format = 'rgba'
301301
self.extra_args = extra_args
302302

@@ -467,6 +467,10 @@ def frame_format(self, frame_format):
467467
if frame_format in self.supported_formats:
468468
self._frame_format = frame_format
469469
else:
470+
cbook._warn_external(
471+
f"Ignoring file format {frame_format!r} which is not "
472+
f"supported by {type(self).__name__}; using "
473+
f"{self.supported_formats[0]} instead.")
470474
self._frame_format = self.supported_formats[0]
471475

472476
def _base_temp_name(self):
@@ -529,7 +533,6 @@ def setup(self, fig, outfile, dpi=None):
529533
self._frames = []
530534

531535
def grab_frame(self, **savefig_kwargs):
532-
from PIL import Image
533536
buf = BytesIO()
534537
self.fig.savefig(
535538
buf, **{**savefig_kwargs, "format": "rgba", "dpi": self.dpi})
@@ -744,15 +747,18 @@ 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

750752
supported_formats = ['png', 'jpeg', 'ppm', 'tiff', 'sgi', 'bmp',
751753
'pbm', 'raw', 'rgba']
752754

753755
def _args(self):
754-
return ([self.bin_path(), '-delay', str(self.delay), '-loop', '0',
755-
'%s*.%s' % (self.temp_prefix, self.frame_format)]
756+
# Force format: ImageMagick does not recognize 'raw'.
757+
fmt = 'rgba:' if self.frame_format == 'raw' else ''
758+
return ([self.bin_path(),
759+
'-size', '%ix%i' % self.frame_size, '-depth', '8',
760+
'-delay', str(self.delay), '-loop', '0',
761+
'%s%s*.%s' % (fmt, self.temp_prefix, self.frame_format)]
756762
+ self.output_args)
757763

758764

0 commit comments

Comments
 (0)