# How to extract 0-255 arrays 3 RGB layers from a gif file?

**URL:** <https://discourse.julialang.org/t/how-to-extract-0-255-arrays-3-rgb-layers-from-a-gif-file/97301>\
**Category:** General Usage\
**Created:** [April 10, 2023, 12:29pm UTC](https://discourse.julialang.org/t/how-to-extract-0-255-arrays-3-rgb-layers-from-a-gif-file/97301 "2023-04-10T12:29:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![programista](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/programista/32/5372_2.png) [@programista](https://discourse.julialang.org/u/programista)\
**Post date:** [April 10, 2023, 12:29pm UTC](https://discourse.julialang.org/t/how-to-extract-0-255-arrays-3-rgb-layers-from-a-gif-file/97301/1 "2023-04-10T12:29:47Z")

</div>

How to extract 0-255 arrays 3 RGB layers from a gif file? What package?

---

<div class="post-metadata">

**Author:** ![fatteneder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatteneder/32/33991_2.png) [@fatteneder](https://discourse.julialang.org/u/fatteneder)\
**Post date:** [April 10, 2023, 12:58pm UTC](https://discourse.julialang.org/t/how-to-extract-0-255-arrays-3-rgb-layers-from-a-gif-file/97301/2 "2023-04-10T12:58:32Z")

</div>

> [@programista](#):
>
> What package?

`FileIO.jl`

---

<div class="post-metadata">

**Author:** ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)\
**Post date:** [April 10, 2023, 2:45pm UTC](https://discourse.julialang.org/t/how-to-extract-0-255-arrays-3-rgb-layers-from-a-gif-file/97301/3 "2023-04-10T14:45:52Z")

</div>

```julia
using Images
img=load("patterns.gif")
imgnew=permutedims(channelview(img), (2,3,1))
size(imgnew)
(626, 626, 3)

```

imgnew:  
626×626×3 Array{N0f8,3} with eltype N0f8

---

<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:** [April 10, 2023, 3:21pm UTC](https://discourse.julialang.org/t/how-to-extract-0-255-arrays-3-rgb-layers-from-a-gif-file/97301/4 "2023-04-10T15:21:01Z")

</div>

The .gif file is a three-dimensional array, of colors `{N0f8}`. Let’s say a temporal sequence of colored matrices (x, y; t).  
To “see” the three layers of a frame, let’s say the 17th, you could do

```julia
red.(img[:,:,17])
green.(img[:,:,17])
blue.(img[:,:,17])

```

or `channelview(img[:,:,17])`

which is equivalent to do

`reinterpret(reshape,N0f8,img[:,:,17])`

```julia

using ImageShow
using TestImages, Colors
img = testimage("mandrill")

mat=reinterpret(reshape,N0f8,img)

redfy=copy(mat)
redfy[2:3,:,:].=0.
reinterpret(reshape,RGB{N0f8},redfy)

greenfy=copy(mat)
greenfy[[1,3],:,:].=0.
reinterpret(reshape,RGB{N0f8},greenfy)

bluefy=copy(mat)
bluefy[1:2,:,:].=0.
reinterpret(reshape,RGB{N0f8},bluefy)

```

> **RGB channels**
>
> ![mandrillo_red](https://global.discourse-cdn.com/julialang/original/3X/e/0/e098f0a2faaaba3bcd0445f490e5b97e7534e234.png)  
> ![mandrillo_green](https://global.discourse-cdn.com/julialang/original/3X/4/a/4a2a487df81887fb0f64ee486cb35022a5f8ef2c.png)
> 
> ![mandrillo_blue](https://global.discourse-cdn.com/julialang/original/3X/3/9/39594a26377c88b44607a1b8f3e9bbeb472ad2a4.png)

---

<div class="post-metadata">

**Author:** ![Ashwani\_Rathee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ashwani_rathee/32/49551_2.png) [@Ashwani\_Rathee](https://discourse.julialang.org/u/Ashwani_Rathee)\
**Post date:** [April 11, 2023, 2:49am UTC](https://discourse.julialang.org/t/how-to-extract-0-255-arrays-3-rgb-layers-from-a-gif-file/97301/5 "2023-04-11T02:49:28Z")

</div>

You need the color palette?

```julia
function gif_palette(filepath::AbstractString)
    error1 = Cint(0)
    gif = LibGif.DGifOpenFileName(filepath, Ref(error1))

    try
        gif == C_NULL && error("failed to open the gif file: null pointer")
        slurp_return = LibGif.DGifSlurp(gif)

        if (slurp_return == LibGif.GIF_ERROR)
            error("failed to read .gif file")
        end

        loaded_gif = unsafe_load(gif)

        img_count = loaded_gif.ImageCount
        components = unsafe_wrap(Array, loaded_gif.SavedImages, loaded_gif.ImageCount)

        # Gif's are palette based and can have upto 256 colors in a image
        colormap = unsafe_load(loaded_gif.SColorMap) # global colormap
        palette = unsafe_wrap(Array, colormap.Colors, colormap.ColorCount)
        return palette
    finally
        LibGif.DGifCloseFile(gif, Ref(error1))
    end
end

```

Given GIFImages.jl is installed and you use things properly. Also to avoid too much bother, you could just read the GIF with FileIO.jl read and then unique(img) on it to get palette.
