# Save matrix of color images

**URL:** <https://discourse.julialang.org/t/save-matrix-of-color-images/92170>\
**Category:** Visualization\
**Tags:** colors, save, animate\
**Created:** [December 26, 2022, 10:08pm UTC](https://discourse.julialang.org/t/save-matrix-of-color-images/92170 "2022-12-26T22:08:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [December 26, 2022, 10:08pm UTC](https://discourse.julialang.org/t/save-matrix-of-color-images/92170/1 "2022-12-26T22:08:00Z")

</div>

With so much effort I managed to simulate (roughly) an animated version of Gonway’s “Game of Life”.

```julia
julia> using Plots,IterTools, Colors

julia> function gol1(n, init=rand(Bool, 10,10))
       
           function gen(m)
               M=copy(m)
               for c in eachindex(m)
                   snb=sum(m[intersect(ci[c] .+ nb, ci)]) - m[c]
                   (m[c] && (snb<2 || snb>3)) ? M[c]=false : (!m[c] && snb==3) ? M[c]=true : nothing # M[c]=m[c]
               end
               M
           end
       
           m=init
           ci=CartesianIndices(m)
           nb=CartesianIndices((-1:1,-1:1))
           xl,xr = 0.5,size(m,1)+.5
           cm=collect(takestrict(iterated(gen, m),n))
          anim= @animate for fr in eachindex(cm)
               heatmap(cm[fr], yflip=true, legend=:none, aspect_ratio = :equual , xlims=(xl,xr), ticks=false);
               vline!(1+xl:xr,c=:red);
               hline!(1+xl:xr,c=:red)
               #display(hline!(1+xl:xr,c=:red))
               #sleep(1)
           end
           gif(anim, "gol1_anim_fps2.gif", fps = 2)
       end
gol1 (generic function with 2 methods)

julia> 
       function init7(n)
           I0=zeros(Bool,9,9)
           I0[:, 5].=true
           repeat(I0, n,n)
       end
init7 (generic function with 1 method)

julia> gol1(20,init7(2))
[ Info: Saved animation to C:\Users\sprmn\.julia 1.8.3\gol1_anim_fps2.gif   

```

![gol1_anim_fps2](https://global.discourse-cdn.com/julialang/original/3X/e/0/e05fe3e4d19a510402c1ac82692b33d0ffd9ba14.gif)  
 ![game of live nX2](https://global.discourse-cdn.com/julialang/original/3X/b/0/b05d76f8a5922fe35db64d58c421aab4a86d2f02.gif)

I found that it is possible to obtain an array of colors by using the Colors package and populating the arrays with values of type RGB, which looks even nicer to me.

 ![cm24](https://global.discourse-cdn.com/julialang/original/3X/6/1/61683f9f7ee89833932c7b26cae24eaeaece27a3.png)

```julia
julia> function gen(m)
           M=copy(m)
           foreach(c -> let
               snb=count(w->w==one(RGB),m[intersect(c .+ CartesianIndices((-1:1,-1:1)),CartesianIndices(m))]) - (m[c]==one(RGB))
               (m[c]==one(RGB) && (snb<2 || snb>3)) ? M[c]=zero(RGB) : (m[c]==zero(RGB) && snb==3) ? M[c]=one(RGB) : nothing
           end , CartesianIndices(m))
           M
       end
gen (generic function with 1 method)

julia> cm=collect(takestrict(iterated(gen, begin I0=zeros(RGB,9,9);
       I0[:, 5].=one(RGB);repeat(I0,2,2) end),30))

```

What I can’t do is get the images in a form that I can save and then create an animated gif with.

---

<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:** [December 27, 2022, 8:14pm UTC](https://discourse.julialang.org/t/save-matrix-of-color-images/92170/2 "2022-12-27T20:14:45Z")

</div>

Does

```julia
using FileIO
save("gol.gif", img; fps=10)

```

work for you? In this case `img` should be a 3d `RGB` or `Gray` array, where the third axis is time.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [December 28, 2022, 10:09am UTC](https://discourse.julialang.org/t/save-matrix-of-color-images/92170/3 "2022-12-28T10:09:02Z")

</div>

And thanks to this I managed to fit everything into

[seven lines,](https://discourse.julialang.org/t/seven-lines-of-julia-examples-sought/50416/189)

even if they were very narrow 😄.

PS  
In fact, I had arrived at this solution a little earlier by better reading one of your answers (very concise/dry in an old post on a similar topic, even if very precise and exhaustive)
