成功解码后
image.Depre()(以及特定的解码功能,如
jpeg.Depre()),返回的值
image.Image。
image.Image是一个接口,用于定义图像的只读视图:它不提供更改/绘制图像的方法。
该
image软件包提供了几种
image.Image实现方式,这些实现方式通常允许您使用一种
Set(x, y int, ccolor.Color)方法来更改/绘制图像。
image.Depre()但是,不能保证返回的图像将是
image包中定义的任何图像类型,甚至
Set()不能保证图像的动态类型具有方法(它可以,但不能保证)。注册的自定义图像解码器可能会返回一个
image.Image值,该值是自定义实现(意味着不是
image包中定义的图像类型)。
如果(图像的动态类型)确实具有
Set()方法,则可以使用类型断言并使用其
Set()方法在其上进行绘制。这是可以做到的:
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.Depre(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方法(返回/提供像素的颜色)中,您将返回如果该图像将是可变的,并在不更改图像的情况下返回原始图像的像素。
image.Image利用 embedding 最容易实现接口,因此您只需要实现所需的更改即可。这是可以做到的:
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照原样加载图像,但是在保存时,请提供我们类型的值,该值将在编码器要求时提供更改后的图像内容(颜色):
jpeg.Enpre(outFile, &MyImg{img}, nil)如果必须更改许多像素,则将所有像素都包含在该
At()方法中是不切实际的。为此,我们可以扩展我们
MyImg的
Set()实现,以存储要更改的像素。示例实现:
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, thenmy := 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.Enpre(outFile, my, nil)如果必须更改许多像素,那么仅创建一个支持更改其像素的新图像可能会更有利可图,例如
image.RGBA,在其上绘制原始图像,然后继续更改您想要的像素。
要将图像绘制到另一个图像上,可以使用该
image/draw包装。
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.Enpre(outFile, cimg, nil)上面的代码仅用于演示。在“现实生活”中,图像
Image.Bounds()可能会返回一个矩形,该矩形并非始于该
(0;0)点,在这种情况下,需要进行一些调整才能使其工作。



