@@ -431,7 +431,7 @@ def __init__(self, *args, **kwargs):
431
431
MovieWriter .__init__ (self , * args , ** kwargs )
432
432
self .frame_format = mpl .rcParams ['animation.frame_format' ]
433
433
434
- def setup (self , fig , outfile , dpi = None , frame_prefix = '_tmp' ,
434
+ def setup (self , fig , outfile , dpi = None , frame_prefix = None ,
435
435
clear_temp = True ):
436
436
'''Perform setup for writing the movie file.
437
437
@@ -446,13 +446,13 @@ def setup(self, fig, outfile, dpi=None, frame_prefix='_tmp',
446
446
controls the size in pixels of the resulting movie file.
447
447
Default is fig.dpi.
448
448
frame_prefix : str, optional
449
- The filename prefix to use for temporary files. Defaults to
450
- ``'_tmp'``.
449
+ The filename prefix to use for temporary files. If None (the
450
+ default), files are written to a temporary directory which is
451
+ deleted by `cleanup` (regardless of the value of *clear_temp*).
451
452
clear_temp : bool, optional
452
453
If the temporary files should be deleted after stitching
453
454
the final result. Setting this to ``False`` can be useful for
454
455
debugging. Defaults to ``True``.
455
-
456
456
'''
457
457
self .fig = fig
458
458
self .outfile = outfile
@@ -461,8 +461,13 @@ def setup(self, fig, outfile, dpi=None, frame_prefix='_tmp',
461
461
self .dpi = dpi
462
462
self ._adjust_frame_size ()
463
463
464
+ if frame_prefix is None :
465
+ self ._tmpdir = TemporaryDirectory ()
466
+ self .temp_prefix = str (Path (self ._tmpdir .name , 'tmp' ))
467
+ else :
468
+ self ._tmpdir = None
469
+ self .temp_prefix = frame_prefix
464
470
self .clear_temp = clear_temp
465
- self .temp_prefix = frame_prefix
466
471
self ._frame_counter = 0 # used for generating sequential file names
467
472
self ._temp_paths = list ()
468
473
self .fname_format_str = '%s%%07d.%s'
@@ -524,13 +529,15 @@ def finish(self):
524
529
525
530
def cleanup (self ):
526
531
MovieWriter .cleanup (self )
527
-
528
- # Delete temporary files
529
- if self .clear_temp :
530
- _log .debug ('MovieWriter: clearing temporary paths=%s' ,
531
- self ._temp_paths )
532
- for path in self ._temp_paths :
533
- path .unlink ()
532
+ if self ._tmpdir :
533
+ _log .debug ('MovieWriter: clearing temporary path=%s' , self ._tmpdir )
534
+ self ._tmpdir .cleanup ()
535
+ else :
536
+ if self .clear_temp :
537
+ _log .debug ('MovieWriter: clearing temporary paths=%s' ,
538
+ self ._temp_paths )
539
+ for path in self ._temp_paths :
540
+ path .unlink ()
534
541
535
542
536
543
@writers .register ('pillow' )
0 commit comments