Image processing 我可以修改图像吗';像素值?

Image processing 我可以修改图像吗';像素值?,image-processing,go,Image Processing,Go,我想在围棋中做这样的事情: for x := 0; x < width; x++ { for y := 0; y < height; y++ { // something similar to this: src_img[x, y] = color.Black } } 对于x:=0;x

我想在围棋中做这样的事情:

for x := 0; x < width; x++ {
    for y := 0; y < height; y++ {
            // something similar to this:
            src_img[x, y] = color.Black
    }
}
对于x:=0;x<宽度;x++{
对于y:=0;y<高度;y++{
//类似于此:
src_img[x,y]=颜色。黑色
}
}
是否可以只导入“图像”、“图像/jpeg”、“图像/颜色”?

例如:

package main

import (
        "fmt"
        "image"
        "image/color"
)

func main() {
        const D = 12
        img := image.NewGray(image.Rect(1, 1, D, D))
        for x := 1; x <= D; x++ {
                img.Set(x, x, color.Gray{byte(2 * x)})
        }
        for x := 1; x < D; x++ {
            fmt.Printf("[%2d, %2d]: %5v\n", x, x, img.At(x, x))
        }
}
src, _, err := image.Decode(file)
if err != nil {
    log.Fatal(err)
}
rgba, ok := src.(*image.RGBA)
if !ok {
    b := src.Bounds()
    rgba = image.NewRGBA(image.Rect(0, 0, b.Dx(), b.Dy()))
    draw.Draw(rgba, rgba.Bounds(), src, b.Min, draw.Src)
}
// rgba is now a *image.RGBA and can be modified freely
rgba.Set(0, 0, color.RGBA{255, 0, 0, 255})

推荐阅读文章(除此之外)。

例如:

package main

import (
        "fmt"
        "image"
        "image/color"
)

func main() {
        const D = 12
        img := image.NewGray(image.Rect(1, 1, D, D))
        for x := 1; x <= D; x++ {
                img.Set(x, x, color.Gray{byte(2 * x)})
        }
        for x := 1; x < D; x++ {
            fmt.Printf("[%2d, %2d]: %5v\n", x, x, img.At(x, x))
        }
}
src, _, err := image.Decode(file)
if err != nil {
    log.Fatal(err)
}
rgba, ok := src.(*image.RGBA)
if !ok {
    b := src.Bounds()
    rgba = image.NewRGBA(image.Rect(0, 0, b.Dx(), b.Dy()))
    draw.Draw(rgba, rgba.Bounds(), src, b.Min, draw.Src)
}
// rgba is now a *image.RGBA and can be modified freely
rgba.Set(0, 0, color.RGBA{255, 0, 0, 255})

推荐阅读文章(除此之外)。

如果要修改图像,该类型是将图像存储在内存中的首选方式。它实现了一个接口,该接口具有设置像素的方便方法:

Set(x, y int, c color.Color)
不幸的是,并非所有解码器都返回RGBA格式的图像。他们中的一些人将图像保留为压缩格式,并不是每个像素都可以修改。对于很多只读用例来说,这是非常快和好的。但是,如果要编辑图像,可能需要复制它。例如:

package main

import (
        "fmt"
        "image"
        "image/color"
)

func main() {
        const D = 12
        img := image.NewGray(image.Rect(1, 1, D, D))
        for x := 1; x <= D; x++ {
                img.Set(x, x, color.Gray{byte(2 * x)})
        }
        for x := 1; x < D; x++ {
            fmt.Printf("[%2d, %2d]: %5v\n", x, x, img.At(x, x))
        }
}
src, _, err := image.Decode(file)
if err != nil {
    log.Fatal(err)
}
rgba, ok := src.(*image.RGBA)
if !ok {
    b := src.Bounds()
    rgba = image.NewRGBA(image.Rect(0, 0, b.Dx(), b.Dy()))
    draw.Draw(rgba, rgba.Bounds(), src, b.Min, draw.Src)
}
// rgba is now a *image.RGBA and can be modified freely
rgba.Set(0, 0, color.RGBA{255, 0, 0, 255})
如果要修改图像,则该类型是将图像存储在内存中的首选方式。它实现了一个接口,该接口具有设置像素的方便方法:

Set(x, y int, c color.Color)
不幸的是,并非所有解码器都返回RGBA格式的图像。他们中的一些人将图像保留为压缩格式,并不是每个像素都可以修改。对于很多只读用例来说,这是非常快和好的。但是,如果要编辑图像,可能需要复制它。例如:

package main

import (
        "fmt"
        "image"
        "image/color"
)

func main() {
        const D = 12
        img := image.NewGray(image.Rect(1, 1, D, D))
        for x := 1; x <= D; x++ {
                img.Set(x, x, color.Gray{byte(2 * x)})
        }
        for x := 1; x < D; x++ {
            fmt.Printf("[%2d, %2d]: %5v\n", x, x, img.At(x, x))
        }
}
src, _, err := image.Decode(file)
if err != nil {
    log.Fatal(err)
}
rgba, ok := src.(*image.RGBA)
if !ok {
    b := src.Bounds()
    rgba = image.NewRGBA(image.Rect(0, 0, b.Dx(), b.Dy()))
    draw.Draw(rgba, rgba.Bounds(), src, b.Min, draw.Src)
}
// rgba is now a *image.RGBA and can be modified freely
rgba.Set(0, 0, color.RGBA{255, 0, 0, 255})

谢谢,但我想修改从文件加载的图像的像素值,而不是创建新的像素值有什么区别?使用image.Decode加载图片,键入assert将其设置为正确的类型,并使用上面所示的.Set方法。谢谢,但我想修改从文件加载的图像的像素值,而不是创建新的像素。有什么区别?使用image.Decode加载图片,键入assert将其设置为正确的类型,并使用上述.Set方法。谢谢您的回答。因此,没有其他方法可以编辑图像而不复制它?我的应用程序将处理图像,但我一定会把它连接到一些C或C++代码。我必须做一些测试,看看Go的反应如何,当我使用例如1600x1000的图片时,效果非常好,但当我使用大而重的图片(5400x3600)时,它会变慢。未来我可能会有大约40张这样的图片,所以复制40张图片变得越来越复杂…谢谢你的回答。因此,没有其他方法可以编辑图像而不复制它?我的应用程序将处理图像,但我一定会把它连接到一些C或C++代码。我必须做一些测试,看看Go的反应如何,当我使用例如1600x1000的图片时,效果非常好,但当我使用大而重的图片(5400x3600)时,它会变慢。未来我可能会有大约40张这样的图片,所以复制40张图片变得越来越复杂。。。