The image I'm using as image.png is:
When running my program with 254, it works and the output is:
Here is my code. If I change the 254 to 255, the output file becomes completely empty (0 bytes in size):
package main
import "fmt"
import "os"
import "bufio"
import "image/png"
import "image"
func main() {
f, _ := os.Open("image.png")
im, _ := png.Decode(bufio.NewReader(f))
img := im.(*image.RGBA)
s := img.Bounds().Size()
w := s.X
h := s.Y
fmt.Println(w, h)
newPix := make([]uint8, len(img.Pix))
iy := 0
for ; iy < h; iy++ {
ix := 0
for ; ix < w; ix++ {
i := img.PixOffset(ix, iy)
r := img.Pix[i+0]
g := img.Pix[i+1]
b := img.Pix[i+2]
a := img.Pix[i+3]
var max uint8 = 254 /*255*/
newPix[i+0] = uint8(max - r)
newPix[i+1] = uint8(max - g)
newPix[i+2] = uint8(max - b)
newPix[i+3] = uint8(max - a)
}
}
newImg := image.NewRGBA(img.Bounds())
newImg.Pix = newPix
o, _ := os.Create("out.png")
defer o.Close()
png.Encode(bufio.NewWriter(o), newImg)
fmt.Println("DONE")
}
Why is this and how can I fix it? 255 is still uint8 so it should work...
I found a discussion on the golang-nuts group where this issue had come up before.
Basically, the output is too small to automatically trigger the writer to flush. Manually flushing the writer (recommended in any case) fixes the issue.
package main
import "fmt"
import "os"
import "bufio"
import "image/png"
import "image"
func main() {
f, _ := os.Open("image.png")
im, _ := png.Decode(bufio.NewReader(f))
img := im.(*image.RGBA)
s := img.Bounds().Size()
w := s.X
h := s.Y
fmt.Println(w, h)
newPix := make([]uint8, len(img.Pix))
iy := 0
for ; iy < h; iy++ {
ix := 0
for ; ix < w; ix++ {
i := img.PixOffset(ix, iy)
r := img.Pix[i+0]
g := img.Pix[i+1]
b := img.Pix[i+2]
a := img.Pix[i+3]
var max uint8 = 255
newPix[i+0] = uint8(max - r)
newPix[i+1] = uint8(max - g)
newPix[i+2] = uint8(max - b)
newPix[i+3] = uint8(max - a)
}
}
newImg := image.NewRGBA(img.Bounds())
newImg.Pix = newPix
o, _ := os.Create("out.png")
defer o.Close()
writer := bufio.NewWriter(o)
png.Encode(writer, newImg)
writer.Flush()
fmt.Println("DONE")
}
Normal caveats regarding ignoring errors apply.
I think it is a mistake to try to change the alpha channel the way you are doing. You will set every transparent part to be visible and every visible part to be transparent if you succeed, and that would make the image look like nonsense.
So try replacing your newPix[i+3]
line with:
newPix[i+3] = a
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With