我正在尝试返回已更改的像素及其颜色。以下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
}
如果有另一种更好或更快的方法来获得所需的输出而不是我愿意接受的。
注意:我是新手。
答案 0 :(得分:-1)
我还没有对此进行测试,没有测试图像。
// 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
}