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

Skip to content

Commit 301c1e7

Browse files
committed
ENH: add support for saving animations as ImageMagick GIF files
1 parent 89482b2 commit 301c1e7

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

lib/matplotlib/animation.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,26 @@ def _args(self):
450450
self.fps)] + self.output_args
451451

452452

453+
# Base class for animated GIFs with convert utility
454+
class ImageMagickBase:
455+
exec_key = 'animation.convert_path'
456+
args_key = 'animation.convert_args'
457+
458+
@property
459+
def output_args(self):
460+
return [self.outfile]
461+
462+
463+
@writers.register('imagemagick_file')
464+
class ImageMagickFileWriter(FileMovieWriter, ImageMagickBase):
465+
supported_formats = ['png']
466+
467+
def _args(self):
468+
delay = 100. / self.fps
469+
return [self.bin_path(), '-delay', str(delay), '-loop', '0',
470+
'%s*.%s' % (self.temp_prefix, self.frame_format)] + self.output_args
471+
472+
453473
class Animation(object):
454474
'''
455475
This class wraps the creation of an animation using matplotlib. It is
@@ -512,7 +532,7 @@ def _stop(self, *args):
512532
self.event_source = None
513533

514534
def save(self, filename, writer=None, fps=None, dpi=None, codec=None,
515-
bitrate=None, extra_args=None, metadata=None, extra_anim=None):
535+
bitrate=None, extra_args=None, metadata=None, extra_anim=None):
516536
'''
517537
Saves a movie file by drawing every frame.
518538

lib/matplotlib/rcsetup.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ def validate_hinting(s):
323323
['xelatex', 'lualatex', 'pdflatex'])
324324

325325
validate_movie_writer = ValidateInStrings('animation.writer',
326-
['ffmpeg', 'ffmpeg_file', 'mencoder', 'mencoder_file'])
326+
['ffmpeg', 'ffmpeg_file', 'mencoder', 'mencoder_file', 'imagemagick_file'])
327327

328328
validate_movie_frame_fmt = ValidateInStrings('animation.frame_format',
329329
['png', 'jpeg', 'tiff', 'raw', 'rgba'])
@@ -621,6 +621,8 @@ def __call__(self, s):
621621
'animation.ffmpeg_args' : ['', validate_stringlist], # Additional arguments for ffmpeg movie writer (using pipes)
622622
'animation.mencoder_path' : ['mencoder', str], # Path to FFMPEG binary. If just binary name, subprocess uses $PATH.
623623
'animation.mencoder_args' : ['', validate_stringlist], # Additional arguments for mencoder movie writer (using pipes)
624+
'animation.convert_path' : ['convert', str], # Path to convert binary. If just binary name, subprocess uses $PATH
625+
'animation.convert_args' : ['', validate_stringlist], # Additional arguments for mencoder movie writer (using pipes)
624626
}
625627

626628
if __name__ == '__main__':

0 commit comments

Comments
 (0)