Home > other >  Why Golang `png.Encode` method makes color data wrong?
Why Golang `png.Encode` method makes color data wrong?

Time:09-21

In Golang, when the pixel value carries the alpha value, it will cause the color value to change. And I try same code in python. No problem!

This is Golang script:

newRgba := image.NewRGBA(image.Rect(0, 0, 1, 1)) //new image
newRgba.SetRGBA(0, 0, color.RGBA{R: 55, G: 23, B: 14, A: 122}) // set pixel value
f, _ := os.Create("./save.png")
defer f.Close()
// save image
png.Encode(f, newRgba)


ff, _ := ioutil.ReadFile("./save.png") //read image
bbb := bytes.NewBuffer(ff)
m, _, _ := image.Decode(bbb)
R, G, B, A := m.At(0, 0).RGBA()
fmt.Println(R>>8,
    G>>8,
    B>>8,
    A>>8,
) // get {55 23 13 122} , it is wrong! Why not {55 23 14 122} ?

This is python script:

from PIL import Image
img = Image.new('RGBA', (1, 1), (55, 23, 14,122)) # set pixel value
img.save('bg.png')


im = Image.open('bg.png')
pix = im.load()

print(pix[0,0])  // get (55, 23, 14, 122)

Why does the result obtained in Golang change?

CodePudding user response:

color.RGBA represents an alpha-premultiplied colour. For pre-multiplied colours, the alpha component represents the maximum value an R/G/B component can take. Hence color.RGBA{122,122,122,122} represents White with alpha 122.

png.Decode returns an image.NRGBA which is not pre-multiplied. This is clearer when the pixel type is displayed. Eg:

fmt.Printf("%#v\n", m.At(0, 0))
// color.NRGBA{R:0xff, G:0xff, B:0xff, A:0x7a}

Your example will work when using NRGBA types.

See this running example: https://play.golang.org/p/69bvYQfkCA_P

Before: color.NRGBA: {55 23 14 122}
After:  color.NRGBA: {55 23 14 122}
  • Related