1

While I was writing test I ran into this strange behaviour that, when given a random image to png.Encode, then decoding the given byte array again with png.Decode it is all zeros (Despite the fact that is a image.NRGBA image - that should be fixed for comparison)

I created a playground for my code here: https://play.golang.org/p/S6XJLVMOLyQ

But for completeness by example code is here:

package main

import (
    "fmt"
    "image"
    "reflect"
    "image/color"
    "bytes"
    "image/png"
    "math/rand"
)

func main() {

    width := 5
    height := 5
    
    upLeft := image.Point{0, 0}
    lowRight := image.Point{width, height}

    inImage := image.NewRGBA(image.Rectangle{upLeft, lowRight})
    
    for x := 0; x < width; x++ {
        for y := 0; y < height; y++ {

            // Colors are defined by Red, Green, Blue, Alpha uint8 values.
            randomColor := color.RGBA{
                uint8(rand.Intn(255)),
                uint8(rand.Intn(255)),
                uint8(rand.Intn(255)),
                0x00}
            inImage.Set(x, y, randomColor)
        }
    }
    
    out := new(bytes.Buffer)
    err := png.Encode(out, inImage)
    
    if err != nil {
        panic("Something went wrong")
    }
    
    reader := bytes.NewReader(out.Bytes())
    outImage, err := png.Decode(reader)
    
    areEqual := reflect.DeepEqual(inImage, outImage)
        
    // Might have to convert it to image.RGBA here - but still all bytes are zero now
    
    fmt.Printf("Are Equal: %s\n\n\texp: %#v\n\n\tgot: %#v\033[39m\n\n", areEqual, inImage, outImage)
}

Which returns:

Are Equal: %!s(bool=false)

exp: &image.RGBA{Pix:[]uint8{0x56, 0x84, 0x7a, 0x0, 0xef, 0x60, 0x9b, 0x0, 0x10, 0x8a, 0x4d, 0x0, 0xfc, 0x76, 0x5e, 0x0, 0x65, 0x5a, 0xa5, 0x0, 0xfe, 0x97, 0x99, 0x0, 0xa7, 0x83, 0x32, 0x0, 0x92, 0xae, 0xb, 0x0, 0xd6, 0x7b, 0xde, 0x0, 0xab, 0x1, 0x17, 0x0, 0x73, 0x9b, 0x3d, 0x0, 0x42, 0x8f, 0x85, 0x0, 0x48, 0xd3, 0x2d, 0x0, 0x3d, 0x54, 0x81, 0x0, 0xce, 0x6b, 0xf8, 0x0, 0x96, 0x77, 0x97, 0x0, 0xed, 0x25, 0x34, 0x0, 0x9c, 0x67, 0xdc, 0x0, 0xcd, 0xfa, 0x53, 0x0, 0xb0, 0x85, 0x8d, 0x0, 0x2a, 0x3b, 0x58, 0x0, 0xd0, 0xb4, 0x96, 0x0, 0x9a, 0x30, 0x2b, 0x0, 0x12, 0x67, 0xaa, 0x0, 0x89, 0x53, 0xde, 0x0}, Stride:20, Rect:image.Rectangle{Min:image.Point{X:0, Y:0}, Max:image.Point{X:5, Y:5}}}

got: &image.NRGBA{Pix:[]uint8{0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}, Stride:20, Rect:image.Rectangle{Min:image.Point{X:0, Y:0}, Max:image.Point{X:5, Y:5}}}[39m

2 Answers 2

3

The problem is that you draw completely transparent pixels (alpha=0), so when encoding it, the png package will "optimize" the image and encode a completely black image (which is the same).

Change the alpha component to 255 being completely opaque:

randomColor := color.RGBA{
    uint8(rand.Intn(255)),
    uint8(rand.Intn(255)),
    uint8(rand.Intn(255)),
    255} // MAX ALPHA

With this simple change output will be (try it on the Go Playground):

Are Equal: true

    exp: &image.RGBA{Pix:[]uint8{0x56, 0x84, 0x7a, 0xff, 0xef, 0x60, 0x9b, 0xff, 0x10, 0x8a, 0x4d, 0xff, 0xfc, 0x76, 0x5e, 0xff, 0x65, 0x5a, 0xa5, 0xff, 0xfe, 0x97, 0x99, 0xff, 0xa7, 0x83, 0x32, 0xff, 0x92, 0xae, 0xb, 0xff, 0xd6, 0x7b, 0xde, 0xff, 0xab, 0x1, 0x17, 0xff, 0x73, 0x9b, 0x3d, 0xff, 0x42, 0x8f, 0x85, 0xff, 0x48, 0xd3, 0x2d, 0xff, 0x3d, 0x54, 0x81, 0xff, 0xce, 0x6b, 0xf8, 0xff, 0x96, 0x77, 0x97, 0xff, 0xed, 0x25, 0x34, 0xff, 0x9c, 0x67, 0xdc, 0xff, 0xcd, 0xfa, 0x53, 0xff, 0xb0, 0x85, 0x8d, 0xff, 0x2a, 0x3b, 0x58, 0xff, 0xd0, 0xb4, 0x96, 0xff, 0x9a, 0x30, 0x2b, 0xff, 0x12, 0x67, 0xaa, 0xff, 0x89, 0x53, 0xde, 0xff}, Stride:20, Rect:image.Rectangle{Min:image.Point{X:0, Y:0}, Max:image.Point{X:5, Y:5}}}

    got: &image.RGBA{Pix:[]uint8{0x56, 0x84, 0x7a, 0xff, 0xef, 0x60, 0x9b, 0xff, 0x10, 0x8a, 0x4d, 0xff, 0xfc, 0x76, 0x5e, 0xff, 0x65, 0x5a, 0xa5, 0xff, 0xfe, 0x97, 0x99, 0xff, 0xa7, 0x83, 0x32, 0xff, 0x92, 0xae, 0xb, 0xff, 0xd6, 0x7b, 0xde, 0xff, 0xab, 0x1, 0x17, 0xff, 0x73, 0x9b, 0x3d, 0xff, 0x42, 0x8f, 0x85, 0xff, 0x48, 0xd3, 0x2d, 0xff, 0x3d, 0x54, 0x81, 0xff, 0xce, 0x6b, 0xf8, 0xff, 0x96, 0x77, 0x97, 0xff, 0xed, 0x25, 0x34, 0xff, 0x9c, 0x67, 0xdc, 0xff, 0xcd, 0xfa, 0x53, 0xff, 0xb0, 0x85, 0x8d, 0xff, 0x2a, 0x3b, 0x58, 0xff, 0xd0, 0xb4, 0x96, 0xff, 0x9a, 0x30, 0x2b, 0xff, 0x12, 0x67, 0xaa, 0xff, 0x89, 0x53, 0xde, 0xff}, Stride:20, Rect:image.Rectangle{Min:image.Point{X:0, Y:0}, Max:image.Point{X:5, Y:5}}}[39m

Note #1: You can't (shouldn't) use reflect.DeepEqual() to compare images, but it's clear from the output that after decoding the pixels are identical.

Note #2: You may pass bytes.Buffer directly to png.Decode(), no need to create another wrapper around it. It implements io.Reader, and its Read() method "serves" the data previously written into it:

outImage, err := png.Decode(out)

Note #3: When you use math/rand, you should seed it with rand.Seed() else you get the same pseudo-random sequence for each run (and not different, random data).

1

You may use:

    inImage := image.NewNRGBA(image.Rectangle{upLeft, lowRight})

with 0 alpha, Try it:

package main

import (
    "bytes"
    "fmt"
    "image"
    "image/color"
    "image/png"
    "log"
    "math/rand"
    "reflect"
)

func main() {
    width := 5
    height := 5
    upLeft := image.Point{0, 0}
    lowRight := image.Point{width, height}
    inImage := image.NewNRGBA(image.Rectangle{upLeft, lowRight})

    for x := 0; x < width; x++ {
        for y := 0; y < height; y++ {
            randomColor := color.RGBA{
                uint8(rand.Intn(255)),
                uint8(rand.Intn(255)),
                uint8(rand.Intn(255)),
                0}
            inImage.Set(x, y, randomColor)
        }
    }

    out := &bytes.Buffer{}
    err := png.Encode(out, inImage)
    if err != nil {
        log.Fatal(err)
    }

    outImage, err := png.Decode(out)
    fmt.Println(reflect.DeepEqual(inImage, outImage)) // true
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.