From ea13624aa11dfa5f26006a4a4fa327b736f7f6cf Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Wed, 15 Dec 2021 00:38:37 +0100 Subject: [PATCH] Refactor common parts of ImageMagick{,File}Writer. ImageMagickWriter previously did not support frame_format = "raw", unlike ImageMagickFileWriter, because it did not normalize it to "rgba". (To test this, start e.g. with the simple_anim.py example, set the writer to an ImageMagickWriter (or ImageMagickFileWriter) and additionally set writer.frame_format = "raw".) Also deprecate the ``delay`` and ``output_args`` properties which can directly be handled by ``_args``, but add an ``input_names`` property which allows the different customization between ImageMagickWriter and ImageMagickFileWriter. --- .../deprecations/21995-AL.rst | 3 ++ lib/matplotlib/animation.py | 47 +++++++++++-------- 2 files changed, 30 insertions(+), 20 deletions(-) create mode 100644 doc/api/next_api_changes/deprecations/21995-AL.rst diff --git a/doc/api/next_api_changes/deprecations/21995-AL.rst b/doc/api/next_api_changes/deprecations/21995-AL.rst new file mode 100644 index 000000000000..e9b7dab027f0 --- /dev/null +++ b/doc/api/next_api_changes/deprecations/21995-AL.rst @@ -0,0 +1,3 @@ +``ImageMagickBase.delay`` and ``ImageMagickBase.output_args`` +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +are deprecated with no replacement. diff --git a/lib/matplotlib/animation.py b/lib/matplotlib/animation.py index 88e79bb4ab4f..446850fe1e62 100644 --- a/lib/matplotlib/animation.py +++ b/lib/matplotlib/animation.py @@ -522,8 +522,8 @@ class FFMpegBase: """ Mixin class for FFMpeg output. - To be useful this must be multiply-inherited from with a - `MovieWriterBase` sub-class. + This is a base class for the concrete `FFMpegWriter` and `FFMpegFileWriter` + classes. """ _exec_key = 'animation.ffmpeg_path' @@ -620,23 +620,42 @@ class ImageMagickBase: """ Mixin class for ImageMagick output. - To be useful this must be multiply-inherited from with a - `MovieWriterBase` sub-class. + This is a base class for the concrete `ImageMagickWriter` and + `ImageMagickFileWriter` classes, which define an ``input_names`` attribute + (or property) specifying the input names passed to ImageMagick. """ _exec_key = 'animation.convert_path' _args_key = 'animation.convert_args' + @_api.deprecated("3.6") @property def delay(self): return 100. / self.fps + @_api.deprecated("3.6") @property def output_args(self): extra_args = (self.extra_args if self.extra_args is not None else mpl.rcParams[self._args_key]) return [*extra_args, self.outfile] + def _args(self): + # ImageMagick does not recognize "raw". + fmt = "rgba" if self.frame_format == "raw" else self.frame_format + extra_args = (self.extra_args if self.extra_args is not None + else mpl.rcParams[self._args_key]) + return [ + self.bin_path(), + "-size", "%ix%i" % self.frame_size, + "-depth", "8", + "-delay", str(100 / self.fps), + "-loop", "0", + f"{fmt}:{self.input_names}", + *extra_args, + self.outfile, + ] + @classmethod def bin_path(cls): binpath = super().bin_path() @@ -662,14 +681,9 @@ class ImageMagickWriter(ImageMagickBase, MovieWriter): Frames are streamed directly to ImageMagick via a pipe and written in a single pass. - """ - def _args(self): - return ([self.bin_path(), - '-size', '%ix%i' % self.frame_size, '-depth', '8', - '-delay', str(self.delay), '-loop', '0', - '%s:-' % self.frame_format] - + self.output_args) + + input_names = "-" # stdin # Combine ImageMagick options with temp file-based writing @@ -683,15 +697,8 @@ class ImageMagickFileWriter(ImageMagickBase, FileMovieWriter): """ supported_formats = ['png', 'jpeg', 'tiff', 'raw', 'rgba'] - - def _args(self): - # Force format: ImageMagick does not recognize 'raw'. - fmt = 'rgba:' if self.frame_format == 'raw' else '' - return ([self.bin_path(), - '-size', '%ix%i' % self.frame_size, '-depth', '8', - '-delay', str(self.delay), '-loop', '0', - '%s%s*.%s' % (fmt, self.temp_prefix, self.frame_format)] - + self.output_args) + input_names = property( + lambda self: f'{self.temp_prefix}*.{self.frame_format}') # Taken directly from jakevdp's JSAnimation package at