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

Skip to content

Commit 4d29cf2

Browse files
authored
Merge pull request #13564 from SBCV/animation_progress
Add an option to log progress while saving animations
2 parents ddb38ae + 24420e9 commit 4d29cf2

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*progress_callback* argument to ani.save()
2+
-------------------------------------------------
3+
4+
The method .FuncAnimation.save() gained an optional
5+
*progress_callback* argument to notify the saving progress.

lib/matplotlib/animation.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -957,7 +957,7 @@ def _stop(self, *args):
957957

958958
def save(self, filename, writer=None, fps=None, dpi=None, codec=None,
959959
bitrate=None, extra_args=None, metadata=None, extra_anim=None,
960-
savefig_kwargs=None):
960+
savefig_kwargs=None, *, progress_callback=None):
961961
"""
962962
Save the animation as a movie file by drawing every frame.
963963
@@ -1013,6 +1013,22 @@ class to use, such as 'ffmpeg'. If ``None``, defaults to
10131013
on to the `savefig` command which is called repeatedly to
10141014
save the individual frames.
10151015
1016+
progress_callback : function, optional
1017+
A callback function that will be called for every frame to notify
1018+
the saving progress. It must have the signature ::
1019+
1020+
def func(current_frame: int, total_frames: int) -> Any
1021+
1022+
where *current_frame* is the current frame number and
1023+
*total_frames* is the total number of frames to be saved.
1024+
*total_frames* is set to None, if the total number of frames can
1025+
not be determined. Return values may exist but are ignored.
1026+
1027+
Example code to write the progress to stdout::
1028+
1029+
progress_callback =\
1030+
lambda i, n: print(f'Saving frame {i} of {n}')
1031+
10161032
Notes
10171033
-----
10181034
*fps*, *codec*, *bitrate*, *extra_args* and *metadata* are used to
@@ -1111,10 +1127,19 @@ class to use, such as 'ffmpeg'. If ``None``, defaults to
11111127
for anim in all_anim:
11121128
# Clear the initial frame
11131129
anim._init_draw()
1130+
frame_number = 0
1131+
save_count_list = [a.save_count for a in all_anim]
1132+
if None in save_count_list:
1133+
total_frames = None
1134+
else:
1135+
total_frames = sum(save_count_list)
11141136
for data in zip(*[a.new_saved_frame_seq() for a in all_anim]):
11151137
for anim, d in zip(all_anim, data):
11161138
# TODO: See if turning off blit is really necessary
11171139
anim._draw_next_frame(d, blit=False)
1140+
if progress_callback is not None:
1141+
progress_callback(frame_number, total_frames)
1142+
frame_number += 1
11181143
writer.grab_frame(**savefig_kwargs)
11191144

11201145
# Reconnect signal for first draw if necessary

0 commit comments

Comments
 (0)