# Plotting a sequence of images as a gif

**URL:** https://discourse.julialang.org/t/plotting-a-sequence-of-images-as-a-gif/23808
**Category:** New to Julia
**Created:** [May 3, 2019, 11:41am UTC](https://discourse.julialang.org/t/plotting-a-sequence-of-images-as-a-gif/23808 "2019-05-03T11:41:13Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![mllera14](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mllera14/32/7967_2.png) [@mllera14](https://discourse.julialang.org/u/mllera14)
#### Post date: [May 3, 2019, 11:41am UTC](https://discourse.julialang.org/t/plotting-a-sequence-of-images-as-a-gif/23808/1 "2019-05-03T11:41:13Z")

</div>

Hi,

I have an input signal parameterized by time, which consists of a time dependent noise mask added on top of a base image defined as:

```julia
I(t) = X + noise(t)

```

I now want to create a gif animation using Plots to see what the signal looks like. The library example tells me to do something like this:

```julia
plt = plot(1)

@gif for i=1:500
        frame = I(i)
        push!(plt, frame)
    end every 10

```

However, frame here is an image, not several variables. I was experimenting with `imshow` but I get an since this does not return a plot object. What is the right way of doing this?

---

<div class="post-metadata">

### Author: ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)
#### Post date: [May 3, 2019, 3:19pm UTC](https://discourse.julialang.org/t/plotting-a-sequence-of-images-as-a-gif/23808/2 "2019-05-03T15:19:49Z")

</div>

Your best bet is probably [Makie.jl](https://github.com/JuliaPlots/Makie.jl), which can do this sort of thing before breakfast (and not just for cat images…) 😹 😺

Deep in the wilder fringes of the Julia ecosystem you’ll find [Luxor.jl](https://github.com/JuliaGraphics/Luxor.jl), which has the words “PNG”, “image”, “noise”, “GIF”, and “animation” in the documentation as this image confirms…

![viking-funeral-for-attobot](https://global.discourse-cdn.com/julialang/original/3X/b/f/bf4b734fe5b845cb1e0ee5fc12562ffecc2c86b8.gif)

… I abandoned this attempt at a pictorial Viking send-off for Attobot a while ago but it’s now found a home. However, I feel that Luxor is designed more for recreational use, and doesn’t provide such obviously essential plotting features such as axes, tick marks, and legends.

(Issues, pull requests, and stars are all welcome!)

---

<div class="post-metadata">

### Author: ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)
#### Post date: [May 3, 2019, 5:11pm UTC](https://discourse.julialang.org/t/plotting-a-sequence-of-images-as-a-gif/23808/3 "2019-05-03T17:11:40Z")

</div>

Did you try to just use `plot(I(i))` inside the loop?

---

<div class="post-metadata">

### Author: ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)
#### Post date: [May 4, 2019, 7:17am UTC](https://discourse.julialang.org/t/plotting-a-sequence-of-images-as-a-gif/23808/4 "2019-05-04T07:17:42Z")

</div>

Another option: just directly `save` a 3d array.

```julia
julia> using FileIO, ImageMagick, Colors, FixedPointNumbers

julia> save("/tmp/test.gif", rand(RGB{N0f8}, 32, 32, 5))

```

---

<div class="post-metadata">

### Author: ![RoyiAvital](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/royiavital/32/571_2.png) [@RoyiAvital](https://discourse.julialang.org/u/RoyiAvital)
#### Post date: [May 4, 2019, 7:48am UTC](https://discourse.julialang.org/t/plotting-a-sequence-of-images-as-a-gif/23808/5 "2019-05-04T07:48:49Z")

</div>

Small remark, You should drop GIF and use [APNG](https://en.wikipedia.org/wiki/APNG).  
It is now supported on all major web browsers and it is much better than GIF.

---

<div class="post-metadata">

### Author: ![yakir12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yakir12/32/297_2.png) [@yakir12](https://discourse.julialang.org/u/yakir12)
#### Post date: [May 4, 2019, 8:34am UTC](https://discourse.julialang.org/t/plotting-a-sequence-of-images-as-a-gif/23808/6 "2019-05-04T08:34:55Z")

</div>

> [@RoyiAvital](#):
>
> use [APNG](https://en.wikipedia.org/wiki/APNG)

Oooo! Cool, thanks for the tip!

---

<div class="post-metadata">

### Author: ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)
#### Post date: [May 4, 2019, 8:42am UTC](https://discourse.julialang.org/t/plotting-a-sequence-of-images-as-a-gif/23808/7 "2019-05-04T08:42:43Z")

</div>

Animated PNGs _look_ like they’d be a better solution than animated GIFs, but I don’t think there’s enough support as regards utilities and editing tools at present. For example, how do you get them to loop?

![this is an animated PNG](https://global.discourse-cdn.com/julialang/original/3X/9/1/91479c2c44ed3a489c09d1e114099f9b4529073c.png)

↑ This pet cthulhu is an animated PNG, but I’ve only ever seen it move once. If it were a serious scientific animation, I’d be disappointed in the AnimatedPNG format.

---

<div class="post-metadata">

### Author: ![RoyiAvital](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/royiavital/32/571_2.png) [@RoyiAvital](https://discourse.julialang.org/u/RoyiAvital)
#### Post date: [May 4, 2019, 9:06am UTC](https://discourse.julialang.org/t/plotting-a-sequence-of-images-as-a-gif/23808/8 "2019-05-04T09:06:08Z")

</div>

All my APNG loop forever.  
I for one usually just wrap my scripts around [APNG Assembler](http://apngasm.sourceforge.net/).  
I write to HD the PNG files and run a command line to wrap them all into APNG.

As you can see you can set the number of loops (Or infinite) etc…

---

<div class="post-metadata">

### Author: ![cormullion](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cormullion/32/49131_2.png) [@cormullion](https://discourse.julialang.org/u/cormullion)
#### Post date: [May 4, 2019, 9:58am UTC](https://discourse.julialang.org/t/plotting-a-sequence-of-images-as-a-gif/23808/9 "2019-05-04T09:58:21Z")

</div>

Ah, that’s cool, thanks - I couldn’t find out how to edit an existing animated PNG, but it’s easy enough to post-process the original still frames again.

It’s clever that you can use the same suffix .PNG for both still and animated images, but it would be good if file managers could show previews (the Finder on macOS doesn’t, but perhaps its different on other platforms…?).

---

<div class="post-metadata">

### Author: ![mllera14](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mllera14/32/7967_2.png) [@mllera14](https://discourse.julialang.org/u/mllera14)
#### Post date: [May 4, 2019, 7:58pm UTC](https://discourse.julialang.org/t/plotting-a-sequence-of-images-as-a-gif/23808/10 "2019-05-04T19:58:39Z")

</div>

> [@feanor12](#):
>
> Did you try to just use `plot(I(i))` inside the loop?

This doesn’t work because `plot` treats the input as an y-axis values with the index in the array being the corresponding x-axis if it is not provided. Reshaping only means that now I have 32 sequences of y-values for 32 x-values.

---

<div class="post-metadata">

### Author: ![mllera14](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mllera14/32/7967_2.png) [@mllera14](https://discourse.julialang.org/u/mllera14)
#### Post date: [May 4, 2019, 7:59pm UTC](https://discourse.julialang.org/t/plotting-a-sequence-of-images-as-a-gif/23808/11 "2019-05-04T19:59:41Z")

</div>

> [@tim.holy](#):
>
> julia\> using FileIO, ImageMagick, Colors, FixedPointNumbers julia\> save(“/tmp/test.gif”, rand(RGB{N0f8}, 32, 32, 5))

This seems like the simplest way, yes. I can just concatenate the frames into one array. Does the time dimension need to be the last one?

> [@RoyiAvital](#):
>
> Small remark, You should drop GIF and use [APNG](https://en.wikipedia.org/wiki/APNG).  
> It is now supported on all major web browsers and it is much better than GIF.

Thanks, this indeed looks useful, though probably an overkill for what I want to do right now which is just look at what I’m doing. Still, probably better for flashier presentations.

---

<div class="post-metadata">

### Author: ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)
#### Post date: [May 5, 2019, 8:37am UTC](https://discourse.julialang.org/t/plotting-a-sequence-of-images-as-a-gif/23808/12 "2019-05-05T08:37:18Z")

</div>

> [@mllera14](#):
>
> Does the time dimension need to be the last one?

Yes.

> just look at what I’m doing

FYI, the various plotting packages (e.g., Makie, Plots, etc) and ImageView all allow you to visualize time-varying images from within Julia. For time-varying images, ImageView will give you a player-widget that you can use to go back and forth in time:

---

<div class="post-metadata">

### Author: ![mllera14](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mllera14/32/7967_2.png) [@mllera14](https://discourse.julialang.org/u/mllera14)
#### Post date: [May 5, 2019, 11:25am UTC](https://discourse.julialang.org/t/plotting-a-sequence-of-images-as-a-gif/23808/13 "2019-05-05T11:25:03Z")

</div>

> [@tim.holy](#):
>
> FYI, the various plotting packages (e.g., Makie, Plots, etc) and ImageView all allow you to visualize time-varying images from within Julia. For time-varying images, ImageView will give you a player-widget that you can use to go back and forth in time:

Oh cool. I did not know that. Thanks.

---

<div class="post-metadata">

### Author: ![feanor12](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/feanor12/32/8212_2.png) [@feanor12](https://discourse.julialang.org/u/feanor12)
#### Post date: [May 5, 2019, 9:17pm UTC](https://discourse.julialang.org/t/plotting-a-sequence-of-images-as-a-gif/23808/14 "2019-05-05T21:17:31Z")

</div>

I did not realize that you want to plot a 2d matrix. I think `heatmap` should work in that case, if I remember correctly.

---

<div class="post-metadata">

### Author: ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)
#### Post date: [October 14, 2020, 8:35am UTC](https://discourse.julialang.org/t/plotting-a-sequence-of-images-as-a-gif/23808/15 "2020-10-14T08:35:08Z")

</div>

Hi,  
is it possible to tune the framerate with animated gifs?

---

<div class="post-metadata">

### Author: ![tim.holy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tim.holy/32/52_2.png) [@tim.holy](https://discourse.julialang.org/u/tim.holy)
#### Post date: [October 14, 2020, 8:37am UTC](https://discourse.julialang.org/t/plotting-a-sequence-of-images-as-a-gif/23808/16 "2020-10-14T08:37:53Z")

</div>

```julia
save("/tmp/test.gif", rand(RGB{N0f8}, 32, 32, 5); fps=5)

```

---

<div class="post-metadata">

### Author: ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)
#### Post date: [October 14, 2020, 8:54am UTC](https://discourse.julialang.org/t/plotting-a-sequence-of-images-as-a-gif/23808/17 "2020-10-14T08:54:10Z")

</div>

Thank you so very much !

---

<div class="post-metadata">

### Author: ![RoyiAvital](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/royiavital/32/571_2.png) [@RoyiAvital](https://discourse.julialang.org/u/RoyiAvital)
#### Post date: [October 16, 2020, 9:26am UTC](https://discourse.julialang.org/t/plotting-a-sequence-of-images-as-a-gif/23808/18 "2020-10-16T09:26:00Z")

</div>

You better use [Animated PNG](https://en.wikipedia.org/wiki/APNG) (APNG). It is much better than GIF while the file size is similar.

It is time to let GIF vanish :-).

---

<div class="post-metadata">

### Author: ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)
#### Post date: [October 16, 2020, 10:00am UTC](https://discourse.julialang.org/t/plotting-a-sequence-of-images-as-a-gif/23808/19 "2020-10-16T10:00:22Z")

</div>

Thanks for the tip !  
The thing is if I do

```julia
save("/tmp/test.png", rand(RGB{N0f8}, 32, 32, 5); fps=5)ype or paste code here

```

instead of gif, then a sequence of pngs is created but the animation is not.  
Creating the animation directly in Julia is handy 😉

---

<div class="post-metadata">

### Author: ![LaurentPlagne](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laurentplagne/32/10103_2.png) [@LaurentPlagne](https://discourse.julialang.org/u/LaurentPlagne)
#### Post date: [October 21, 2020, 5:14pm UTC](https://discourse.julialang.org/t/plotting-a-sequence-of-images-as-a-gif/23808/20 "2020-10-21T17:14:41Z")

</div>

Thank you again.  
The created animated gif is perfect but I wonder If I could display it directly inside vscode (not via ImageView or clicking on the gif file) ?

[Next page](https://discourse.julialang.org/t/plotting-a-sequence-of-images-as-a-gif/23808.md?page=2)
