gpt4 book ai didi

go - 将 RGBA 转换为 NRGBA

转载 作者:数据小太阳 更新时间:2023-10-29 03:29:52 25 4
gpt4 key购买 nike

我正在尝试返回已更改的像素及其颜色。以下 func 工作正常,但它没有给我所需的 255,255,255 值。是否可以将其转换为所需的格式?

我已经看过这里的文档了 -> https://golang.org/pkg/image/color/

我也手动尝试过不同的转换,但我无法让它工作。有人知道如何在 golang 中转换它吗?

type Pixel struct {
x, y int
r, g, b, a uint32
}

func diffImages(imgOne *image.RGBA, imgTwo *image.RGBA) []Pixel {
var pixels []Pixel

bounds := imgOne.Bounds()
diff := false

for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {

r, g, b, a := imgOne.At(x, y).RGBA()
rt, gt, bt, at := imgTwo.At(x, y).RGBA()

if r != rt || g != gt || b != bt || a != at {
diff=true
}

if diff == true {
pixel := new(Pixel)
pixel.x = x
pixel.y = y
pixel.r = rt
pixel.g = gt
pixel.b = bt
pixel.a = at
pixels = append(pixels, *pixel)
}

diff = false
}
}
return pixels
}

如果有其他比我愿意接受的更好或更快的方法来获得所需的输出。

注意:我是新来的。

最佳答案

你是这个意思吗?我做了其他重构,您的代码似乎不必要地复杂。

我没有测试过这个,没有测试图像。

// Pixels are pixels.
type Pixel struct {
x, y int
color color.NRGBA
}

func diffImages(imgOne image.RGBA, imgTwo image.RGBA) []Pixel {
var pixels []Pixel

bounds := imgOne.Bounds()

for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {

if !reflect.DeepEqual(imgOne.Pix, imgTwo.Pix) {

rt, gt, bt, at := imgTwo.At(x, y).RGBA()
pixel := new(Pixel)
pixel.x = x
pixel.y = y
pixel.color.R = uint8(rt)
pixel.color.G = uint8(gt)
pixel.color.B = uint8(bt)
pixel.color.A = uint8(at)
pixels = append(pixels, *pixel)
}
}
}
return pixels
}

关于go - 将 RGBA 转换为 NRGBA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40069550/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com