更改单个像素的颜色-Golang图片 [英] Change color of a single pixel - Golang image

查看:139
本文介绍了更改单个像素的颜色-Golang图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想打开jpeg图像文件,对其进行编码,更改一些像素颜色,然后将其保存为原样。

I want to open jpeg image file, encode it, change some pixel colors, and then save it back as it was.

我想做点什么像这样

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

img,err := jpeg.Decode(imgfile)
if err != nil {
    fmt.Println(err.Error())
}
img.Set(0, 0, color.RGBA{85, 165, 34, 1})
img.Set(1,0,....)

outFile, _ := os.Create("changed.jpeg")
defer outFile.Close()
jpeg.Encode(outFile, img, nil)

我可以找不到可行的解决方案,因为我在对图像文件进行编码后获得的默认图像类型没有Set方法。

I just can't come up with a working solution, since default image type that I get after encoding image-file doesn't have Set method.

有人可以解释如何做到这一点吗?非常感谢。

Can anyone explain how to do this? Thanks a lot.

推荐答案

成功解码后 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 包,甚至图像的动态类型都具有 Set()方法(它可以,但不能保证)。注册的自定义图像解码器可能会返回 image.Image 值作为自定义实现(这意味着 image 包)。

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 ,但是以其 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()方法。为此,我们可以扩展我们的 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)点,在这种情况下,需要进行一些调整才能使其正常工作。

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.

这篇关于更改单个像素的颜色-Golang图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆