draxu26480 2014-03-01 02:27
浏览 136
已采纳

Golang PNG颜色处理不会达到255

The image I'm using as image.png is:

enter image description here

When running my program with 254, it works and the output is:

enter image description here

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...

  • 写回答

2条回答 默认 最新

  • dt20081409 2014-03-01 03:14
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100