首页 > 解决方案 > 添加 Alpha 通道性能

问题描述

我正在寻找将我的数组转换为 PNG 图像。它目前是 RGB8 编码的图像。我可以使用以下代码执行此操作:

s2 := make([]uint8, 2048*2448*3)
err = dset.Read(&s2)
if err != nil {
    panic(err)
}

var (
    width  = 2048
    height = 2448
    rgb    = 3
)

to1D := func(x, y int) int {
    return (x * height * rgb) + (rgb * y)
}

img := image.NewRGBA(image.Rect(0, 0, width, height))
for ix := 0; ix < width; ix++ {
    for iy := 0; iy < height; iy++ {
        cords := to1D(ix, iy)
        img.SetRGBA(ix, iy, color.RGBA{R: s2[cords], G: s2[cords+1], B: s2[cords+2], A: 255})
    }
}

有没有更好的方法来添加不涉及循环遍历每个像素并单独设置它的 Alpha 通道?

谢谢!

标签: imagenumpygo

解决方案


推荐阅读