29
29
# * http://pymedia.org/
30
30
# * libmng (produces swf) python wrappers: https://github.com/libming/libming
31
31
# * Wrap x264 API:
32
- # http://stackoverflow.com/questions/2940671/how-to-encode-series-of-images-into-h264-using-x264-api-c-c
32
+
33
+ # (http://stackoverflow.com/questions/2940671/
34
+ # how-to-encode-series-of-images-into-h264-using-x264-api-c-c )
33
35
34
36
35
37
# A registry for available MovieWriter classes
@@ -83,7 +85,7 @@ class MovieWriter(object):
83
85
The format used in writing frame data, defaults to 'rgba'
84
86
'''
85
87
def __init__ (self , fps = 5 , codec = None , bitrate = None , extra_args = None ,
86
- metadata = None ):
88
+ metadata = None ):
87
89
'''
88
90
Construct a new MovieWriter object.
89
91
@@ -199,11 +201,12 @@ def grab_frame(self, **savefig_kwargs):
199
201
# Tell the figure to save its data to the sink, using the
200
202
# frame format and dpi.
201
203
self .fig .savefig (self ._frame_sink (), format = self .frame_format ,
202
- dpi = self .dpi , ** savefig_kwargs )
204
+ dpi = self .dpi , ** savefig_kwargs )
203
205
except RuntimeError :
204
206
out , err = self ._proc .communicate ()
205
- verbose .report ('MovieWriter -- Error running proc:\n %s\n %s' % (out ,
206
- err ), level = 'helpful' )
207
+ verbose .report ('MovieWriter -- Error '
208
+ 'running proc:\n %s\n %s' % (out ,
209
+ err ), level = 'helpful' )
207
210
raise
208
211
209
212
def _frame_sink (self ):
@@ -217,10 +220,10 @@ def _args(self):
217
220
def cleanup (self ):
218
221
'Clean-up and collect the process used to write the movie file.'
219
222
out , err = self ._proc .communicate ()
220
- verbose .report ('MovieWriter -- Command stdout: \n %s' % out ,
221
- level = 'debug' )
222
- verbose .report ('MovieWriter -- Command stderr: \n %s' % err ,
223
- level = 'debug' )
223
+ verbose .report ('MovieWriter -- '
224
+ 'Command stdout: \n %s' % out , level = 'debug' )
225
+ verbose .report ('MovieWriter -- '
226
+ 'Command stderr: \n %s' % err , level = 'debug' )
224
227
225
228
@classmethod
226
229
def bin_path (cls ):
@@ -238,8 +241,10 @@ def isAvailable(cls):
238
241
running the commandline tool.
239
242
'''
240
243
try :
241
- subprocess .Popen (cls .bin_path (), shell = False ,
242
- stdout = subprocess .PIPE , stderr = subprocess .PIPE )
244
+ subprocess .Popen (cls .bin_path (),
245
+ shell = False ,
246
+ stdout = subprocess .PIPE ,
247
+ stderr = subprocess .PIPE )
243
248
return True
244
249
except OSError :
245
250
return False
@@ -307,7 +312,7 @@ def _frame_sink(self):
307
312
self ._temp_names .append (fname )
308
313
verbose .report (
309
314
'FileMovieWriter.frame_sink: saving frame %d to fname=%s' %
310
- (self ._frame_counter , fname ),
315
+ (self ._frame_counter , fname ),
311
316
level = 'debug' )
312
317
self ._frame_counter += 1 # Ensures each created name is 'unique'
313
318
@@ -325,8 +330,8 @@ def finish(self):
325
330
# the process here, rather than having an open pipe.
326
331
if self ._proc .returncode :
327
332
raise RuntimeError ('Error creating movie, return code: '
328
- + str (self ._proc .returncode )
329
- + ' Try running with --verbose-debug' )
333
+ + str (self ._proc .returncode )
334
+ + ' Try running with --verbose-debug' )
330
335
331
336
def cleanup (self ):
332
337
MovieWriter .cleanup (self )
@@ -405,6 +410,7 @@ class AVConvBase(FFMpegBase):
405
410
class AVConvWriter (AVConvBase , FFMpegWriter ):
406
411
pass
407
412
413
+
408
414
# Combine AVConv options with file-based writing
409
415
@writers .register ('avconv_file' )
410
416
class AVConvFileWriter (AVConvBase , FFMpegFileWriter ):
@@ -420,7 +426,7 @@ class MencoderBase:
420
426
# Mencoder only allows certain keys, other ones cause the program
421
427
# to fail.
422
428
allowed_metadata = ['name' , 'artist' , 'genre' , 'subject' , 'copyright' ,
423
- 'srcform' , 'comment' ]
429
+ 'srcform' , 'comment' ]
424
430
425
431
# Mencoder mandates using name, but 'title' works better with ffmpeg.
426
432
# If we find it, just put it's value into name
@@ -439,8 +445,8 @@ def output_args(self):
439
445
args .extend (self .extra_args )
440
446
if self .metadata :
441
447
args .extend (['-info' , ':' .join ('%s=%s' % (k , v )
442
- for k , v in self .metadata .items ()
443
- if k in self .allowed_metadata )])
448
+ for k , v in self .metadata .items ()
449
+ if k in self .allowed_metadata )])
444
450
return args
445
451
446
452
@@ -451,9 +457,9 @@ def _args(self):
451
457
# Returns the command line parameters for subprocess to use
452
458
# mencoder to create a movie
453
459
return [self .bin_path (), '-' , '-demuxer' , 'rawvideo' , '-rawvideo' ,
454
- ('w=%i:h=%i:' % self .frame_size +
455
- 'fps=%i:format=%s' % (self .fps ,
456
- self .frame_format ))] + self .output_args
460
+ ('w=%i:h=%i:' % self .frame_size +
461
+ 'fps=%i:format=%s' % (self .fps ,
462
+ self .frame_format ))] + self .output_args
457
463
458
464
459
465
# Combine Mencoder options with temp file-based writing
@@ -465,10 +471,10 @@ def _args(self):
465
471
# Returns the command line parameters for subprocess to use
466
472
# mencoder to create a movie
467
473
return [self .bin_path (),
468
- 'mf://%s*.%s' % (self .temp_prefix , self .frame_format ),
469
- '-frames' , str (self ._frame_counter ), '-mf' ,
470
- 'type=%s:fps=%d' % (self .frame_format ,
471
- self .fps )] + self .output_args
474
+ 'mf://%s*.%s' % (self .temp_prefix , self .frame_format ),
475
+ '-frames' , str (self ._frame_counter ), '-mf' ,
476
+ 'type=%s:fps=%d' % (self .frame_format ,
477
+ self .fps )] + self .output_args
472
478
473
479
474
480
# Base class for animated GIFs with convert utility
@@ -622,21 +628,26 @@ def save(self, filename, writer=None, fps=None, dpi=None, codec=None,
622
628
if savefig_kwargs is None :
623
629
savefig_kwargs = {}
624
630
625
- # FIXME: Using 'bbox_inches' doesn't currently work with writers that pipe
626
- # the data to the command because this requires a fixed frame size (see
627
- # Ryan May's reply in this thread: [1]). Thus we drop the 'bbox_inches'
628
- # argument if it exists in savefig_kwargs.
631
+ # FIXME: Using 'bbox_inches' doesn't currently work with
632
+ # writers that pipe the data to the command because this
633
+ # requires a fixed frame size (see Ryan May's reply in this
634
+ # thread: [1]). Thus we drop the 'bbox_inches' argument if it
635
+ # exists in savefig_kwargs.
629
636
#
630
- # [1] http://matplotlib.1069221.n5.nabble.com/Animation-class-let-save-accept-kwargs-which-are-passed-on-to-savefig-td39627.html
637
+ # [1] (http://matplotlib.1069221.n5.nabble.com/
638
+ # Animation-class-let-save-accept-kwargs-which-
639
+ # are-passed-on-to-savefig-td39627.html)
631
640
#
632
- if savefig_kwargs . has_key ( 'bbox_inches' ) :
641
+ if 'bbox_inches' in savefig_kwargs :
633
642
if not (writer in ['ffmpeg_file' , 'mencoder_file' ] or
634
- isinstance (writer , (FFMpegFileWriter , MencoderFileWriter ))):
635
- print ("Warning: discarding the 'bbox_inches' argument in " \
636
- "'savefig_kwargs' as it is only currently supported " \
637
- "with the writers 'ffmpeg_file' and 'mencoder_file' " \
638
- "(writer used: '{}')." .format (writer if isinstance (writer , str )
639
- else writer .__class__ .__name__ ))
643
+ isinstance (writer ,
644
+ (FFMpegFileWriter , MencoderFileWriter ))):
645
+ print ("Warning: discarding the 'bbox_inches' argument in "
646
+ "'savefig_kwargs' as it is only currently supported "
647
+ "with the writers 'ffmpeg_file' and 'mencoder_file' "
648
+ "(writer used: "
649
+ "'{}')." .format (writer if isinstance (writer , str )
650
+ else writer .__class__ .__name__ ))
640
651
savefig_kwargs .pop ('bbox_inches' )
641
652
642
653
# Need to disconnect the first draw callback, since we'll be doing
@@ -677,7 +688,8 @@ def save(self, filename, writer=None, fps=None, dpi=None, codec=None,
677
688
if is_string_like (writer ):
678
689
if writer in writers .avail :
679
690
writer = writers [writer ](fps , codec , bitrate ,
680
- extra_args = extra_args , metadata = metadata )
691
+ extra_args = extra_args ,
692
+ metadata = metadata )
681
693
else :
682
694
import warnings
683
695
warnings .warn ("MovieWriter %s unavailable" % writer )
@@ -708,7 +720,7 @@ def save(self, filename, writer=None, fps=None, dpi=None, codec=None,
708
720
# Reconnect signal for first draw if necessary
709
721
if reconnect_first_draw :
710
722
self ._first_draw_id = self ._fig .canvas .mpl_connect ('draw_event' ,
711
- self ._start )
723
+ self ._start )
712
724
713
725
def _step (self , * args ):
714
726
'''
@@ -756,7 +768,7 @@ def _pre_draw(self, framedata, blit):
756
768
def _draw_frame (self , framedata ):
757
769
# Performs actual drawing of the frame.
758
770
raise NotImplementedError ('Needs to be implemented by subclasses to'
759
- ' actually make an animation.' )
771
+ ' actually make an animation.' )
760
772
761
773
def _post_draw (self , framedata , blit ):
762
774
# After the frame is rendered, this handles the actual flushing of
@@ -799,7 +811,7 @@ def _setup_blit(self):
799
811
self ._blit_cache = dict ()
800
812
self ._drawn_artists = []
801
813
self ._resize_id = self ._fig .canvas .mpl_connect ('resize_event' ,
802
- self ._handle_resize )
814
+ self ._handle_resize )
803
815
self ._post_draw (None , self ._blit )
804
816
805
817
def _handle_resize (self , * args ):
@@ -821,7 +833,7 @@ def _end_redraw(self, evt):
821
833
self .event_source .start ()
822
834
self ._fig .canvas .mpl_disconnect (self ._resize_id )
823
835
self ._resize_id = self ._fig .canvas .mpl_connect ('resize_event' ,
824
- self ._handle_resize )
836
+ self ._handle_resize )
825
837
826
838
827
839
class TimedAnimation (Animation ):
@@ -836,7 +848,7 @@ class TimedAnimation(Animation):
836
848
the animation.
837
849
'''
838
850
def __init__ (self , fig , interval = 200 , repeat_delay = None , repeat = True ,
839
- event_source = None , * args , ** kwargs ):
851
+ event_source = None , * args , ** kwargs ):
840
852
# Store the timing information
841
853
self ._interval = interval
842
854
self ._repeat_delay = repeat_delay
@@ -952,13 +964,13 @@ class FuncAnimation(TimedAnimation):
952
964
953
965
*init_func* is a function used to draw a clear frame. If not given, the
954
966
results of drawing from the first item in the frames sequence will be
955
- used. This function will be called once before the first frame.
967
+ used. This function will be called once before the first frame.
956
968
957
969
If blit=True, *func* and *init_func* should return an iterable of
958
970
drawables to clear.
959
971
'''
960
972
def __init__ (self , fig , func , frames = None , init_func = None , fargs = None ,
961
- save_count = None , ** kwargs ):
973
+ save_count = None , ** kwargs ):
962
974
if fargs :
963
975
self ._args = fargs
964
976
else :
0 commit comments