Write out an animation incrementally

Until now, I have been using the following approach to create animations:

using Plots
anim = Animation();
for it = 1:nt
    # Computations
    # ...

    # Visualization (e.g. every 10th time step)
    if mod(it,10)==0
gif(anim, "test.gif", fps = 15);

Once, the simulation has finished, this creates an animated gif.

How can I modify this code to have it write out the animation incrementally after each call to frame(anim) instead of having it written only at the end?

Then, one could see already during the simulation if it goes as expected or not and in case of a crash one would at least have a partial animation to inspect.


E.g. in MATLAB this is possible with imwrite, using the WriteMode append:


The full MATLAB example is here.

1 Like

Move the call to gif inside your loop?

Thanks @nilshg for your reply. However, if I just move the call to gif inside the loop, it will each time write the whole existing animation as a gif (overwriting) instead of appending to a preexisting gif.

Sorry I haven’t tried this myself and am on the phone but it seems to me the gif writes out the anim object with whatever frames are in it at that point in time - so if your loop adds to the anim object incrementally and you write it out every now
and then wouldn’t it just include all frames up to that point?

I might be missing something here but unless worrying the whole gif is super expensive it should be fine to just overwrite it every now and then?

Yes, that’s exactly what I meant and what seems to be the case.

I want to be able to write high resolution gifs; that is, one frame might reach the order of Megabytes and thus an animation from a simulation of multiple hours might reach the order of Gigabytes. Therefore, if the animation reaches Gigabytes, I cannot write out the whole animation each time.

particular reason insisting using gif?

No, actually not. It could be a priori any kind of format that can contain an animation.

@jling, do you have a solution to write out the animation incrementally in another format?

When I need such a capability (fast incremental saving) I would just write multiple files that I later merge. What you want to do in general should be possible though I don’t know what is the right tool but in the mean time this generic workaround should work.

1 Like

I second what yuyichao says, maybe you can just write out individual image files and then make them video or gif later.

I believe that’s actually how Plots’ animations work — they write out their individual frames into a temporary directory and then punt to some external tool (I think Imagemagick or ffmpeg) that combines them into the gif.

FWIW, incrementally adding frames to a gif isn’t really a great match for the file format as there are a number of “whole movie” sorts of limitations/optimizations/compressions that require knowing about all frames — for example adding a frame that introduces a new color might require re-writing the header and all prior frames.

That is indeed true and if you open the anim.dir directory before you enter the loop you can watch the frames individually while the simulation is running.

Thanks to all for the inputs!

I absolutely get your points and agree. If I would consider only performance, then I would just write images and at the end create an animation. Yet, I am looking for some kind of interactivity, that is to conveniently be able to see what the simulation does while it is ongoing. So, I need to create/update an animation every now and then… and an incremental write to an animation seems a priory better then just to recreate the animation from scratch every time.

Would you know any movie file format that is better suited for incremental write?

Maybe it would work well for gif with a particular set of parameters? (Given that MATLAB has implemented the incremental gif write…)