# 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:** 14\
**Page:** 2

<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 21, 2020, 5:21pm UTC](https://discourse.julialang.org/t/plotting-a-sequence-of-images-as-a-gif/23808/21 "2020-10-21T17:21:04Z")

</div>

> [@LaurentPlagne](#):
>
> 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 😉

Well, I guess this is an attractive feature request :-).  
For me, I think it is easy enough to automate this by wrapping [APNG Assembler](http://apngasm.sourceforge.net/) with some OS level calls.

---

<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:25pm UTC](https://discourse.julialang.org/t/plotting-a-sequence-of-images-as-a-gif/23808/22 "2020-10-21T17:25:59Z")

</div>

I tried apng assembler but I have been a bit disappointed : it was much longer to run than saving to gif ( about a minute vs a few seconds) and the resulting png was twice bigger than the gif without an obvious quality enhancement…

---

<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 21, 2020, 5:31pm UTC](https://discourse.julialang.org/t/plotting-a-sequence-of-images-as-a-gif/23808/23 "2020-10-21T17:31:01Z")

</div>

If your color palette is narrow they should be similar quality wise. Yet APNG should have better compression. As far as I can remember to the least.

---

<div class="post-metadata">

**Author:** ![Obtuya7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/obtuya7/32/17606_2.png) [@Obtuya7](https://discourse.julialang.org/u/Obtuya7)\
**Post date:** [November 9, 2020, 10:26pm UTC](https://discourse.julialang.org/t/plotting-a-sequence-of-images-as-a-gif/23808/24 "2020-11-09T22:26:58Z")

</div>

I’m trying to create a .gif from several images too. The save function doesn’t work for my array although I feel like this is functionally similar to the example. Any recommendations?

```julia
using FileIO, ImageIO, Colors, FixedPointNumbers, Images

folder = 
A = Array{Array{RGB{Normed{UInt8,8}},2},1}()

for i in 1:200
    img_path = "tmp/"*string(i)*".png"
    img = load(img_path)
    push!(A,img)
end

save("test.gif", A)

```

```julia
┌ Warning: Mapping to the storage type failed; perhaps your data had out-of-range values?
│ Try `map(clamp01nan, img)` to clamp values to a valid range.
└ @ ImageMagick ImageMagick\0LwpT\src\ImageMagick.jl:180
Error encountered while saving "test.gif".

Fatal error:
ERROR: LoadError: MethodError: no method matching mapIM(::Array{RGB{Normed{UInt8,8}},2})
Closest candidates are:
  mapIM(::Bool) at \ImageMagick\0LwpT\src\ImageMagick.jl:264
  mapIM(::UInt16) \ImageMagick\0LwpT\src\ImageMagick.jl:262
  mapIM(::UInt32) \ImageMagick\0LwpT\src\ImageMagick.jl:263
  ...

```

---

<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:** [November 9, 2020, 10:36pm UTC](https://discourse.julialang.org/t/plotting-a-sequence-of-images-as-a-gif/23808/25 "2020-11-09T22:36:06Z")

</div>

It should be a 3d array (HWT, T = time), not a vector-of-matrices.

---

<div class="post-metadata">

**Author:** ![Obtuya7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/obtuya7/32/17606_2.png) [@Obtuya7](https://discourse.julialang.org/u/Obtuya7)\
**Post date:** [November 10, 2020, 12:07am UTC](https://discourse.julialang.org/t/plotting-a-sequence-of-images-as-a-gif/23808/26 "2020-11-10T00:07:55Z")

</div>

Yes. Thank you.

```julia
A = cat(A, load(img_path), dims=3)

```

---

<div class="post-metadata">

**Author:** ![BuddhiLW](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/buddhilw/32/33168_2.png) [@BuddhiLW](https://discourse.julialang.org/u/BuddhiLW)\
**Post date:** [August 1, 2022, 3:17pm UTC](https://discourse.julialang.org/t/plotting-a-sequence-of-images-as-a-gif/23808/27 "2022-08-01T15:17:19Z")

</div>

Actually, if you zoom and open on another tab, you can see it move once.

---

<div class="post-metadata">

**Author:** ![dustyirwin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dustyirwin/32/14996_2.png) [@dustyirwin](https://discourse.julialang.org/u/dustyirwin)\
**Post date:** [October 31, 2022, 11:12pm UTC](https://discourse.julialang.org/t/plotting-a-sequence-of-images-as-a-gif/23808/28 "2022-10-31T23:12:16Z")

</div>

Thanks for this tip! I think using this format just solved a big problem I’m having with regular GIFs and the headaches that come along with indexed colors.

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [December 16, 2023, 10:31pm UTC](https://discourse.julialang.org/t/plotting-a-sequence-of-images-as-a-gif/23808/29 "2023-12-16T22:31:12Z")

</div>

> [@Obtuya7](#):
>
> `A = cat(A, load(img_path), dims=3)`

This is not working for me:

```julia
using FileIO, ImageIO, Colors, FixedPointNumbers, Images

folder = "video"
A = Array{Array{RGB{Normed{UInt8,8}},2},1}()

for j in 0:66
    global A
    local img
    img_path = folder*"/"*"img-"*lpad(j,4,"0")*".png"
    img=load(img_path)
    A = cat(A, img, dims=3)
end

save("video/Tether.gif", A)

```

Error message:

```julia
julia> include("src/export_gif.jl")
ERROR: LoadError: DimensionMismatch: mismatch in dimension 1 (expected 0 got 480)
Stacktrace:
  [1] _cs
    @ Base ./abstractarray.jl:1782 [inlined]
  [2] _cshp
    @ Base ./abstractarray.jl:1778 [inlined]
  [3] _cat_size_shape(::Tuple{Bool, Bool, Bool}, ::Tuple{Int64, Int64, Int64}, ::Matrix{RGBA{N0f8}})
    @ Base ./abstractarray.jl:1758
  [4] cat_size_shape
    @ ./abstractarray.jl:1756 [inlined]
  [5] _cat_t
    @ ./abstractarray.jl:1797 [inlined]
  [6] _cat(::Int64, ::Vector{Matrix{RGB{N0f8}}}, ::Matrix{RGBA{N0f8}})
    @ Base ./abstractarray.jl:1793
  [7] #cat#159
    @ Base ./abstractarray.jl:1990 [inlined]
  [8] top-level scope
    @ ~/repos/Tethers.jl/src/export_gif.jl:11
  [9] include(fname::String)
    @ Base.MainInclude ./client.jl:489
 [10] top-level scope
    @ REPL[3]:1
in expression starting at /home/ufechner/repos/Tethers.jl/src/export_gif.jl:6

```

What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [December 16, 2023, 10:37pm UTC](https://discourse.julialang.org/t/plotting-a-sequence-of-images-as-a-gif/23808/30 "2023-12-16T22:37:08Z")

</div>

OK, this works:

```julia
using FileIO, ImageIO, Colors, FixedPointNumbers, Images

folder = "video"
A = Array{Array{RGB{Normed{UInt8,8}},2},1}()

for j in 0:66
    global A
    local img
    img_path = folder*"/"*"img-"*lpad(j,4,"0")*".png"
    img=load(img_path)
    if j==0
        A=img
    else
        A = cat(A, img, dims=3)
    end
end

save(folder * "/Tether.gif", A)

```

---

<div class="post-metadata">

**Author:** ![sefffal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sefffal/32/23640_2.png) [@sefffal](https://discourse.julialang.org/u/sefffal)\
**Post date:** [February 15, 2024, 10:44pm UTC](https://discourse.julialang.org/t/plotting-a-sequence-of-images-as-a-gif/23808/31 "2024-02-15T22:44:34Z")

</div>

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

This snippet (which I have used hundreds of times, so useful!) seems to have stopped working.

It now gives a message to the effect of

```julia
===========================================
UndefVarError: `libwand` not defined
===========================================

Fatal error:
ERROR: BoundsError: attempt to access 4×32×32×5 reinterpret(UInt8, ::Array{N0f8,4}) at index [1:4, 1:32, 1]
Stacktrace:
  [1] throw_boundserror(A::Base.ReinterpretArray{…}, I::Tuple{…})
    @ Base ./abstractarray.jl:734
  [2] checkbounds
    @ ./abstractarray.jl:699 [inlined]
  [3] _getindex
    @ ./multidimensional.jl:888 [inlined]
  [4] getindex
    @ ./abstractarray.jl:1288 [inlined]
  [5] save_(f::FileIO.File{…}, img::Array{…}; permute_horizontal::Bool, mapi::typeof(identity))
    @ QuartzImageIO ~/.julia/packages/QuartzImageIO/4Fkqk/src/QuartzImageIO.jl:310

```

Is this a QuartzImageIO.jl, ImageMagick.jl, or FileIO.jl issue, do we think?

---

<div class="post-metadata">

**Author:** ![George9000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/george9000/32/23619_2.png) [@George9000](https://discourse.julialang.org/u/George9000)\
**Post date:** [February 15, 2024, 11:07pm UTC](https://discourse.julialang.org/t/plotting-a-sequence-of-images-as-a-gif/23808/32 "2024-02-15T23:07:31Z")

</div>

This [issue](https://github.com/JuliaIO/ImageMagick.jl/issues/209) seems relevant to libwand error.

---

<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:** [February 16, 2024, 8:50am UTC](https://discourse.julialang.org/t/plotting-a-sequence-of-images-as-a-gif/23808/33 "2024-02-16T08:50:36Z")

</div>

It’s _both_ ImageMagick (the source of the `libwand` error) and QuartzImageIO (the source of the `BoundsError`). I’m guessing the ImageMagick\_jll version may have changed, you could try pinning it perhaps.

Fundamentally, ImageMagick has been a never-ending source of trouble. You might try [GitHub - JuliaIO/GIFImages.jl: Provides Gif support in Julia using LibGif](https://github.com/ashwani-rathee/GIFImages.jl) and see if that works better.

---

<div class="post-metadata">

**Author:** ![e3c6](https://avatars.discourse-cdn.com/v4/letter/e/e79b87/32.png) [@e3c6](https://discourse.julialang.org/u/e3c6)\
**Post date:** [February 16, 2024, 9:52am UTC](https://discourse.julialang.org/t/plotting-a-sequence-of-images-as-a-gif/23808/34 "2024-02-16T09:52:10Z")

</div>

> [@RoyiAvital](#):
>
> wrapping [APNG Assembler](http://apngasm.sourceforge.net/)

Is there a `_jll` for APNG Assembler?

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