# How to convert 2D array to RGB image?

This is the code using Python:

``````# Channel01,02,03 are all 2D uint16 arrays with length of 2748*2748.
# The trick here is to convert to gray image firstly,and then merge the gray images into a RGB image...
# I don't know the reason behind it , but it works...
r = Image.fromarray(Channel03*0.1).convert('L')
g = Image.fromarray(Channel02*0.1).convert('L')
b = Image.fromarray(Channel01*0.1).convert('L')
image_output = Image.merge("RGB", (r, g, b))
image_output.save("map_picture.bmp)
``````

I tried to implement the same bahaviour in Go:

``````    rect := image.Rect(0, 0, 2748, 2748)
imgSet := image.NewRGBA64(rect)
for i := 0; i < length; i++ {
x := i % 2748
y := i / 2748
pixel := color.RGBA64{
// channel1,2,3 are the same with the py code
R: channel3[i]/10,
G: channel2[i]/10,
B: channel1[i]/10,
A: 65535,
}
imgSet.Set(x, y, pixel)
}

outFile, err := os.Create("hybrid.bmp")
if err != nil {
t.Error(err)
return
}
defer outFile.Close()
err = bmp.Encode(outFile, imgSet)
if err != nil {
t.Error(err)
return
}
}
``````

The comparision result is as following,the image generated by Go seems very lossy…

So,how to obtain the same result using Go?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.