且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

更改单个像素的颜色-Golang图片

更新时间:2023-11-09 14:58:52

成功解码后 image.Decode() (还有特定的解码功能,如 jpeg.Decode() )返回 image.Image image.Image 是一个接口,用于定义图像的只读视图:它不提供更改/绘制图像的方法。

On successful decoding image.Decode() (and also specific decoding functions like jpeg.Decode()) return a value of image.Image. image.Image is an interface which defines a read-only view of an image: it does not provide methods to change / draw on the image.

image 包提供了几个 image.Image 实现,可用于更改/绘制在图像上,通常使用 Set(x,y int,c color.Color)方法。

The image package provides several image.Image implementations which allow you to change / draw on the image, usually with a Set(x, y int, c color.Color) method.

image.Decode()但是不能保证返回的图像将是 image $ c $中定义的任何图像类型。 c>包,甚至图像的动态类型都具有 Set()方法(它可以,但不能保证)。注册的自定义图像解码器可能会返回 image.Image 值作为自定义实现(这意味着 image $ c $中未定义的图像类型c>包)。

image.Decode() however does not give you any guarantee that the returned image will be any of the image types defined in the image package, or even that the dynamic type of the image has a Set() method (it may, but no guarantee). Registered custom image decoders may return you an image.Image value being a custom implementation (meaning not an image type defined in the image package).

如果(动态类型的)图像确实具有 Set()方法,您可以使用类型断言并使用其 Set()绘制它的方法。这是可以做到的:

If the (dynamic type of the) image does have a Set() method, you may use type assertion and use its Set() method to draw on it. This is how it can be done:

type Changeable interface {
    Set(x, y int, c color.Color)
}

imgfile, err := os.Open("unchanged.jpg")
if err != nil {
    panic(err.Error())
}
defer imgfile.Close()

img, err := jpeg.Decode(imgfile)
if err != nil {
    panic(err.Error())
}

if cimg, ok := img.(Changeable); ok {
    // cimg is of type Changeable, you can call its Set() method (draw on it)
    cimg.Set(0, 0, color.RGBA{85, 165, 34, 255})
    cimg.Set(0, 1, color.RGBA{255, 0, 0, 255})
    // when done, save img as usual
} else {
    // No luck... see your options below
}

如果图像确实没有 Set()方法,您可以选择通过实现实现 image.Image $ c的自定义类型来覆盖其视图 $ c>,但是以其 At(x,y int)color.Color 方法(返回/提供像素的颜色),您将返回要设置的新颜色如果图像可以更改,则返回原始图像的像素(不更改图像)。

If the image does not have a Set() method, you may choose to "override its view" by implementing a custom type which implements image.Image, but in its At(x, y int) color.Color method (which returns / supplies the colors of pixels) you return the new colors that you would set if the image would be changeable, and return the pixels of the original image where you would not change the image.

实现 image.Image 接口最容易通过使用 embeddding 完成,因此您只需要实现所需的更改即可。这是可以做到的:

Implementing the image.Image interface is easiest done by utilizing embedding, so you only need to implement the changes you want. This is how it can be done:

type MyImg struct {
    // Embed image.Image so MyImg will implement image.Image
    // because fields and methods of Image will be promoted:
    image.Image
}

func (m *MyImg) At(x, y int) color.Color {
    // "Changed" part: custom colors for specific coordinates:
    switch {
    case x == 0 && y == 0:
        return color.RGBA{85, 165, 34, 255}
    case x == 0 && y == 1:
        return color.RGBA{255, 0, 0, 255}
    }
    // "Unchanged" part: the colors of the original image:
    return m.Image.At(x, y)
}

使用它:非常简单。照原样加载图像,但是在保存时,请提供我们的 MyImg 类型的值,该值将在用户要求时提供更改后的图像内容(颜色)。编码器:

Using it: extremely simple. Load the image as you did, but when saving, provide a value of our MyImg type which will take care of providing the altered image content (colors) when it is asked by the encoder:

jpeg.Encode(outFile, &MyImg{img}, nil)

如果必须更改许多像素,将所有内容都包含在 At()$ c $中是不切实际的c>方法。为此,我们可以扩展我们的 MyImg 以使我们的 Set()实现能够存储要更改的像素。实现示例:

If you have to change many pixels, it's not practical to include all in the At() method. For that we can extend our MyImg to have our Set() implementation which stores the pixels that we want to change. Example implementation:

type MyImg struct {
    image.Image
    custom map[image.Point]color.Color
}

func NewMyImg(img image.Image) *MyImg {
    return &MyImg{img, map[image.Point]color.Color{}}
}

func (m *MyImg) Set(x, y int, c color.Color) {
    m.custom[image.Point{x, y}] = c
}

func (m *MyImg) At(x, y int) color.Color {
    // Explicitly changed part: custom colors of the changed pixels:
    if c := m.custom[image.Point{x, y}]; c != nil {
        return c
    }
    // Unchanged part: colors of the original image:
    return m.Image.At(x, y)
}

使用它:

// Load image as usual, then

my := NewMyImg(img)
my.Set(0, 0, color.RGBA{85, 165, 34, 1})
my.Set(0, 1, color.RGBA{255, 0, 0, 255})

// And when saving, save 'my' instead of the original:
jpeg.Encode(outFile, my, nil)

更改许多像素,那么仅创建支持更改其像素的新图像可能会更有利可图,例如 image.RGBA ,绘制原始

If you have to change many pixels, then it might be more profitable to just create a new image which supports changing its pixels, e.g. image.RGBA, draw the original image on it and then proceed to change pixels you want to.

要将图像绘制到另一个图像上,可以使用 图像/绘图 包。

To draw an image onto another, you can use the image/draw package.

cimg := image.NewRGBA(img.Bounds())
draw.Draw(cimg, img.Bounds(), img, image.Point{}, draw.Over)

// Now you have cimg which contains the original image and is changeable
// (it has a Set() method)
cimg.Set(0, 0, color.RGBA{85, 165, 34, 255})
cimg.Set(0, 1, color.RGBA{255, 0, 0, 255})

// And when saving, save 'cimg' of course:
jpeg.Encode(outFile, cimg, nil)

以上代码仅用于演示。在现实生活中,图像 Image.Bounds()可能会返回一个以(0; 0)$ c $开头的矩形c>点,在这种情况下,需要进行一些调整才能使其正常工作。

The above code is just for demonstration. In "real-life" images Image.Bounds() may return a rectangle that does not start at (0;0) point, in which case some adjustment would be needed to make it work.