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

Skip to content

Commit bc04c8a

Browse files
authored
Merge pull request #16082 from anntzer/animation-tmpdir
Defaut to writing animation frames to a temporary directory.
2 parents ca1330c + cc3482f commit bc04c8a

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

doc/api/next_api_changes/behaviour.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,9 @@ Setting the same property under multiple aliases now raises a TypeError
5656
Previously, calling e.g. ``plot(..., color=somecolor, c=othercolor)`` would
5757
emit a warning because ``color`` and ``c`` actually map to the same Artist
5858
property. This now raises a TypeError.
59+
60+
`.FileMovieWriter` temporary frames directory
61+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
62+
`.FileMovieWriter` now defaults to writing temporary frames in a temporary
63+
directory, which is always cleared at exit. In order to keep the individual
64+
frames saved on the filesystem, pass an explicit *frame_prefix*.

lib/matplotlib/animation.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ def __init__(self, *args, **kwargs):
433433
MovieWriter.__init__(self, *args, **kwargs)
434434
self.frame_format = mpl.rcParams['animation.frame_format']
435435

436-
def setup(self, fig, outfile, dpi=None, frame_prefix='_tmp',
436+
def setup(self, fig, outfile, dpi=None, frame_prefix=None,
437437
clear_temp=True):
438438
"""
439439
Perform setup for writing the movie file.
@@ -449,13 +449,13 @@ def setup(self, fig, outfile, dpi=None, frame_prefix='_tmp',
449449
controls the size in pixels of the resulting movie file.
450450
Default is fig.dpi.
451451
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*).
454455
clear_temp : bool, optional
455456
If the temporary files should be deleted after stitching
456457
the final result. Setting this to ``False`` can be useful for
457458
debugging. Defaults to ``True``.
458-
459459
"""
460460
self.fig = fig
461461
self.outfile = outfile
@@ -464,8 +464,13 @@ def setup(self, fig, outfile, dpi=None, frame_prefix='_tmp',
464464
self.dpi = dpi
465465
self._adjust_frame_size()
466466

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
467473
self.clear_temp = clear_temp
468-
self.temp_prefix = frame_prefix
469474
self._frame_counter = 0 # used for generating sequential file names
470475
self._temp_paths = list()
471476
self.fname_format_str = '%s%%07d.%s'
@@ -527,13 +532,15 @@ def finish(self):
527532

528533
def cleanup(self):
529534
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()
537544

538545

539546
@writers.register('pillow')

0 commit comments

Comments
 (0)