Create an image from matrix

i am using gans to generate an image, it ouputs an array of size (256,256,3,1) “WHCB” how can build an image out of it ?

here is an example what i tried, i think i need to remove dim ( channels and batch):

function create_img(data)
#data → (witdh hight channel batch_size)
img_array = permutedims(vcat(reshape.(chunk(hr,256),256, :)…),(2,3,1))
return img_array .* 255.0f0

using Images

data = rand(256,256,3,1)

create_images(data) = colorview.(RGB, permutedims.(eachslice(data; dims=4), Ref((3,1,2))))

eachslice iterates over each 256x256x3 array in the batch.
permutedims moves the color channel first to create a 3x256x256 array.
colorview turns the 256x256x3 array of Float64 into a 256x256 array of RGB values.

If you have ImageInTerminal installed, you even get a nice preview displayed!

1 Like


when should i multiply data by 255 ? because the data is normalized pixels/255

The RGB{Float64} color type uses 0.0-1.0 scaled values. This may be helpful: