Skip to content

Commit 94dec18

Browse files
committed
Merge pull request matplotlib#1337 from jakevdp/gif-animation
ENH: allow animations to be saved as animated GIFs
2 parents 89482b2 + ab80b7e commit 94dec18

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

lib/matplotlib/animation.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,41 @@ 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 delay(self):
460+
return 100. / self.fps
461+
462+
@property
463+
def output_args(self):
464+
return [self.outfile]
465+
466+
467+
@writers.register('imagemagick')
468+
class ImageMagickWriter(MovieWriter, ImageMagickBase):
469+
def _args(self):
470+
return ([self.bin_path(),
471+
'-size', '%ix%i' % self.frame_size, '-depth', '8',
472+
'-delay', str(self.delay), '-loop', '0',
473+
'%s:-' % self.frame_format]
474+
+ self.output_args)
475+
476+
477+
@writers.register('imagemagick_file')
478+
class ImageMagickFileWriter(FileMovieWriter, ImageMagickBase):
479+
supported_formats = ['png', 'jpeg', 'ppm', 'tiff', 'sgi', 'bmp',
480+
'pbm', 'raw', 'rgba']
481+
482+
def _args(self):
483+
return ([self.bin_path(), '-delay', str(self.delay), '-loop', '0',
484+
'%s*.%s' % (self.temp_prefix, self.frame_format)]
485+
+ self.output_args)
486+
487+
453488
class Animation(object):
454489
'''
455490
This class wraps the creation of an animation using matplotlib. It is
@@ -512,7 +547,7 @@ def _stop(self, *args):
512547
self.event_source = None
513548

514549
def save(self, filename, writer=None, fps=None, dpi=None, codec=None,
515-
bitrate=None, extra_args=None, metadata=None, extra_anim=None):
550+
bitrate=None, extra_args=None, metadata=None, extra_anim=None):
516551
'''
517552
Saves a movie file by drawing every frame.
518553

lib/matplotlib/rcsetup.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,8 @@ 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',
327+
'imagemagick', 'imagemagick_file'])
327328

328329
validate_movie_frame_fmt = ValidateInStrings('animation.frame_format',
329330
['png', 'jpeg', 'tiff', 'raw', 'rgba'])
@@ -621,6 +622,8 @@ def __call__(self, s):
621622
'animation.ffmpeg_args' : ['', validate_stringlist], # Additional arguments for ffmpeg movie writer (using pipes)
622623
'animation.mencoder_path' : ['mencoder', str], # Path to FFMPEG binary. If just binary name, subprocess uses $PATH.
623624
'animation.mencoder_args' : ['', validate_stringlist], # Additional arguments for mencoder movie writer (using pipes)
625+
'animation.convert_path' : ['convert', str], # Path to convert binary. If just binary name, subprocess uses $PATH
626+
'animation.convert_args' : ['', validate_stringlist], # Additional arguments for mencoder movie writer (using pipes)
624627
}
625628

626629
if __name__ == '__main__':

0 commit comments

Comments
 (0)