Skip to content

Commit ea13624

Browse files
committed
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.
1 parent 80a839e commit ea13624

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
``ImageMagickBase.delay`` and ``ImageMagickBase.output_args``
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
are deprecated with no replacement.

lib/matplotlib/animation.py

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -522,8 +522,8 @@ class FFMpegBase:
522522
"""
523523
Mixin class for FFMpeg output.
524524
525-
To be useful this must be multiply-inherited from with a
526-
`MovieWriterBase` sub-class.
525+
This is a base class for the concrete `FFMpegWriter` and `FFMpegFileWriter`
526+
classes.
527527
"""
528528

529529
_exec_key = 'animation.ffmpeg_path'
@@ -620,23 +620,42 @@ class ImageMagickBase:
620620
"""
621621
Mixin class for ImageMagick output.
622622
623-
To be useful this must be multiply-inherited from with a
624-
`MovieWriterBase` sub-class.
623+
This is a base class for the concrete `ImageMagickWriter` and
624+
`ImageMagickFileWriter` classes, which define an ``input_names`` attribute
625+
(or property) specifying the input names passed to ImageMagick.
625626
"""
626627

627628
_exec_key = 'animation.convert_path'
628629
_args_key = 'animation.convert_args'
629630

631+
@_api.deprecated("3.6")
630632
@property
631633
def delay(self):
632634
return 100. / self.fps
633635

636+
@_api.deprecated("3.6")
634637
@property
635638
def output_args(self):
636639
extra_args = (self.extra_args if self.extra_args is not None
637640
else mpl.rcParams[self._args_key])
638641
return [*extra_args, self.outfile]
639642

643+
def _args(self):
644+
# ImageMagick does not recognize "raw".
645+
fmt = "rgba" if self.frame_format == "raw" else self.frame_format
646+
extra_args = (self.extra_args if self.extra_args is not None
647+
else mpl.rcParams[self._args_key])
648+
return [
649+
self.bin_path(),
650+
"-size", "%ix%i" % self.frame_size,
651+
"-depth", "8",
652+
"-delay", str(100 / self.fps),
653+
"-loop", "0",
654+
f"{fmt}:{self.input_names}",
655+
*extra_args,
656+
self.outfile,
657+
]
658+
640659
@classmethod
641660
def bin_path(cls):
642661
binpath = super().bin_path()
@@ -662,14 +681,9 @@ class ImageMagickWriter(ImageMagickBase, MovieWriter):
662681
663682
Frames are streamed directly to ImageMagick via a pipe and written
664683
in a single pass.
665-
666684
"""
667-
def _args(self):
668-
return ([self.bin_path(),
669-
'-size', '%ix%i' % self.frame_size, '-depth', '8',
670-
'-delay', str(self.delay), '-loop', '0',
671-
'%s:-' % self.frame_format]
672-
+ self.output_args)
685+
686+
input_names = "-" # stdin
673687

674688

675689
# Combine ImageMagick options with temp file-based writing
@@ -683,15 +697,8 @@ class ImageMagickFileWriter(ImageMagickBase, FileMovieWriter):
683697
"""
684698

685699
supported_formats = ['png', 'jpeg', 'tiff', 'raw', 'rgba']
686-
687-
def _args(self):
688-
# Force format: ImageMagick does not recognize 'raw'.
689-
fmt = 'rgba:' if self.frame_format == 'raw' else ''
690-
return ([self.bin_path(),
691-
'-size', '%ix%i' % self.frame_size, '-depth', '8',
692-
'-delay', str(self.delay), '-loop', '0',
693-
'%s%s*.%s' % (fmt, self.temp_prefix, self.frame_format)]
694-
+ self.output_args)
700+
input_names = property(
701+
lambda self: f'{self.temp_prefix}*.{self.frame_format}')
695702

696703

697704
# Taken directly from jakevdp's JSAnimation package at

0 commit comments

Comments
 (0)