How to extract 0-255 arrays 3 RGB layers from a gif file? What package?
FileIO.jl
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
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
red.(img[:,:,17])
green.(img[:,:,17])
blue.(img[:,:,17])
or channelview(img[:,:,17])
which is equivalent to do
reinterpret(reshape,N0f8,img[:,:,17])
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)
You need the color palette?
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.