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