# Write out an animation incrementally

**URL:** <https://discourse.julialang.org/t/write-out-an-animation-incrementally/25410>\
**Category:** Visualization\
**Tags:** question, plots, animations\
**Created:** [June 18, 2019, 3:16pm UTC](https://discourse.julialang.org/t/write-out-an-animation-incrementally/25410 "2019-06-18T15:16:38Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![samo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samo/32/35398_2.png) [@samo](https://discourse.julialang.org/u/samo)\
**Post date:** [June 18, 2019, 3:16pm UTC](https://discourse.julialang.org/t/write-out-an-animation-incrementally/25410/1 "2019-06-18T15:16:38Z")

</div>

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

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

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

```

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

**Question**  
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?

**Motivation**  
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.

Thanks!

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

```julia
imwrite(imind,cm,filename,'gif','WriteMode','append');

```

The full MATLAB example is [here](https://ch.mathworks.com/matlabcentral/answers/94495-how-can-i-create-animated-gif-images-in-matlab).

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [June 19, 2019, 6:23am UTC](https://discourse.julialang.org/t/write-out-an-animation-incrementally/25410/2 "2019-06-19T06:23:49Z")

</div>

Move the call to `gif` inside your loop?

---

<div class="post-metadata">

**Author:** ![samo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samo/32/35398_2.png) [@samo](https://discourse.julialang.org/u/samo)\
**Post date:** [June 19, 2019, 7:03am UTC](https://discourse.julialang.org/t/write-out-an-animation-incrementally/25410/3 "2019-06-19T07:03:31Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [June 19, 2019, 7:12am UTC](https://discourse.julialang.org/t/write-out-an-animation-incrementally/25410/4 "2019-06-19T07:12:37Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![samo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samo/32/35398_2.png) [@samo](https://discourse.julialang.org/u/samo)\
**Post date:** [June 19, 2019, 7:28am UTC](https://discourse.julialang.org/t/write-out-an-animation-incrementally/25410/5 "2019-06-19T07:28:08Z")

</div>

> [@nilshg](#):
>
> 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?

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

> [@nilshg](#):
>
> unless worrying the whole gif is super expensive it should be fine to just overwrite it every now and then?

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.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [June 19, 2019, 7:51am UTC](https://discourse.julialang.org/t/write-out-an-animation-incrementally/25410/6 "2019-06-19T07:51:08Z")

</div>

particular reason insisting using gif?

---

<div class="post-metadata">

**Author:** ![samo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samo/32/35398_2.png) [@samo](https://discourse.julialang.org/u/samo)\
**Post date:** [June 19, 2019, 7:54am UTC](https://discourse.julialang.org/t/write-out-an-animation-incrementally/25410/7 "2019-06-19T07:54:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![samo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samo/32/35398_2.png) [@samo](https://discourse.julialang.org/u/samo)\
**Post date:** [June 19, 2019, 1:44pm UTC](https://discourse.julialang.org/t/write-out-an-animation-incrementally/25410/8 "2019-06-19T13:44:38Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [June 19, 2019, 2:20pm UTC](https://discourse.julialang.org/t/write-out-an-animation-incrementally/25410/9 "2019-06-19T14:20:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [June 19, 2019, 2:23pm UTC](https://discourse.julialang.org/t/write-out-an-animation-incrementally/25410/10 "2019-06-19T14:23:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [June 19, 2019, 2:38pm UTC](https://discourse.julialang.org/t/write-out-an-animation-incrementally/25410/11 "2019-06-19T14:38:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![BeastyBlacksmith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/beastyblacksmith/32/4741_2.png) [@BeastyBlacksmith](https://discourse.julialang.org/u/BeastyBlacksmith)\
**Post date:** [June 19, 2019, 2:55pm UTC](https://discourse.julialang.org/t/write-out-an-animation-incrementally/25410/12 "2019-06-19T14:55:29Z")

</div>

> [@mbauman](#):
>
> 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.

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.

---

<div class="post-metadata">

**Author:** ![samo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samo/32/35398_2.png) [@samo](https://discourse.julialang.org/u/samo)\
**Post date:** [June 19, 2019, 5:05pm UTC](https://discourse.julialang.org/t/write-out-an-animation-incrementally/25410/13 "2019-06-19T17:05:02Z")

</div>

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.

> [@mbauman](#):
>
> 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.

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…)
