8000 Refactor common parts of ImageMagick{,File}Writer. · matplotlib/matplotlib@1f6ed7e · GitHub
[go: up one dir, main page]

Skip to content

Commit 1f6ed7e

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 1f6ed7e

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
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: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -620,23 +620,38 @@ 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+
To be useful this must be multiply-inherited from with a `MovieWriterBase`
624+
sub-class, and define an ``input_names`` attribute (or property) specifying
625+
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 [self.bin_path(),
649+
"-size", "%ix%i" % self.frame_size, "-depth", "8",
650+
"-delay", str(100 / self.fps), "-loop", "0",
651+
f"{fmt}:{self.input_names}",
652+
*extra_args,
653+
self.outfile]
654+
640655
@classmethod
641656
def bin_path(cls):
642657
binpath = super().bin_path()
@@ -662,14 +677,9 @@ class ImageMagickWriter(ImageMagickBase, MovieWriter):
662677
663678
Frames are streamed directly to ImageMagick via a pipe and written
664679
in a single pass.
665-
666680
"""
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)
681+
682+
input_names = "-" # stdin
673683

674684

675685
# Combine ImageMagick options with temp file-based writing
@@ -683,15 +693,8 @@ class ImageMagickFileWriter(ImageMagickBase, FileMovieWriter):
683693
"""
684694

685695
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)
696+
input_names = property(
697+
lambda self: f'{self.temp_prefix}*.{self.frame_format}')
695698

696699

697700
# Taken directly from jakevdp's JSAnimation package at

0 commit comments

Comments
 (0)
0