英文:
Using the Set() method of an image.Image or *image.RGBA
问题
我现在是你的中文翻译。以下是翻译好的内容:
我在这个夏天的空闲时间里,正在使用Go图像包进行一些练习。
package main
import (
"os"
"image"
"image/png"
"image/color"
"log"
"fmt"
"reflect"
)
func main(){
file , err := os.OpenFile("C:/Sources/go3x3.png", os.O_RDWR, os.FileMode(0777))
if err != nil {
log.Fatal(err)
}
img , err := png.Decode(file)
if err != nil {
log.Fatal(err)
}
img.At(0,0).RGBA()
fmt.Println("type:", reflect.TypeOf(img))
m := image.NewRGBA(image.Rect(0, 0, 640, 480))
fmt.Println("type:", reflect.TypeOf(m))
m.Set(5, 5, color.RGBA{255, 0, 0, 255})
img.Set(0, 0, color.RGBA{136, 0, 21, 255})
}
问题在于,当我将img.Set
注释掉后运行代码,得到的结果是:
type: *image.RGBA
type: *image.RGBA
但是当我取消注释后,就会出现错误,提示:
img.Set undefined (type image.Image has no field or method Set)
我猜测我可能在使用reflect
时出错了,我对Go中的接口和类型定义还没有完全掌握。
英文:
I am doing some practice with the Go image package with my free time this summer.
package main
import (
"os"
"image"
"image/png"
"image/color"
"log"
"fmt"
"reflect"
)
func main(){
file , err := os.OpenFile("C:/Sources/go3x3.png", os.O_RDWR, os.FileMode(0777))
if err != nil {
log.Fatal(err)
}
img , err := png.Decode(file)
if err != nil {
log.Fatal(err)
}
img.At(0,0).RGBA()
fmt.Println("type:", reflect.TypeOf(img))
m := image.NewRGBA(image.Rect(0, 0, 640, 480))
fmt.Println("type:", reflect.TypeOf(m))
m.Set(5, 5, color.RGBA{255, 0, 0, 255})
img.Set(0, 0, color.RGBA{136, 0, 21, 255})
}
The problem here is when I run it with the img.Set
commented out I get this result
type: *image.RGBA
type: *image.RGBA
but when it's uncommented I get an error saying
img.Set undefined (type image.Image has no field or method Set)
I'm assuming I'm using reflect wrong, I'm still fully grasping the whole interface and type definitions in Go.
答案1
得分: 10
要扩展之前的答案,png.Decode
可能会创建多种不同的底层图像类型(*image.Gray
、*image.RGBA
、*image.Paletted
、*image.NRGBA
等等)。它将创建的图像作为 image.Image
接口返回,该接口提供只读访问数据的功能。
然而,大多数实际返回的图像类型都实现了 Set
方法,用于简单的写入访问。你可以通过现有的 image/draw
包中的 draw.Image
接口来安全地测试和使用该方法。代码如下:
// From image/draw:
// Image is an image.Image with a Set method to change a single pixel.
type Image interface {
image.Image
Set(x, y int, c color.Color)
}
func drawablePNGImage(r io.Reader) (draw.Image, error) {
img, err := png.Decode(r)
if err != nil {
return nil, err
}
dimg, ok := img.(draw.Image)
if !ok {
return nil, fmt.Errorf("%T is not a drawable image type", img)
}
return dimg, nil
}
<kbd>Playground</kbd>(显示了调用所有 image.Image
方法和 Set
方法的示例)。
针对 Go1.17+ 进行编辑:
请注意,Go1.17 添加了 draw.RGBA64Image
并提供了 SetRGBA64
方法。与 draw.Image
类似,所有标准图像类型都实现了该方法。该方法的优点是颜色值不会被封装在 color.Color
接口类型中,因此执行多个像素操作可能更快。
还请注意,Go1.18 添加了优化,用于实现可选的 draw.RGBA64Image
和 image.RGBA64Image
接口的图像的 draw.Draw
和 draw.DrawMask
回退实现。
英文:
To expand on
a previous answer
answer:
png.Decode
may create one of several different underlying image types (*image.Gray
, *image.RGBA
, *image.Paletted
, *image.NRGBA
, etc).
It returns whatever image it created as an image.Image
interface which provides read only access to the data.
However, all (most?) of the actual image types it returns do implement the Set
method for simple write access.
The way you can safely test for and use this method is via the existing draw.Image
interface from the image/draw
package. It's just this:
// From image/draw:
// Image is an image.Image with a Set method to change a single pixel.
type Image interface {
image.Image
Set(x, y int, c color.Color)
}
So you could do something like:
func drawablePNGImage(r io.Reader) (draw.Image, error) {
img, err := png.Decode(r)
if err != nil {
return nil, err
}
dimg, ok := img.(draw.Image)
if !ok {
return nil, fmt.Errorf("%T is not a drawable image type", img)
}
return dimg, nil
}
<kbd>Playground</kbd> (shows an example calling all the image.Image
methods as well as Set
).
Edit for Go1.17+:
Note that Go1.17 added draw.RGBA64Image
with a SetRGBA64
method. As with draw.Image
, all of the standard image types implement this. The advantage of this method is that the color values are not boxed in the color.Color
interface type so doing many pixel operations can be faster.
Also note that Go1.18 added optimisations to the draw.Draw
and draw.DrawMask
fallback implementations for images that implement the optional draw.RGBA64Image
and image.RGBA64Image
interfaces.
答案2
得分: 3
reflect.TypeOf(img)
可以获取接口img
中值的反射类型,如果它是一个接口的话。在这种情况下,img
是一个image.Image
接口,其中包含一个*image.RGBA
。
你可以通过将img
转换为*image.RGBA
来修复你的代码,或者更健壮地定义一个具有正确Set
方法的接口类型,并将img
转换为该类型(正如@DaveC所指出的,在"image/draw"
中的draw.Image
接口非常适合此用途)。如果你不能确定png.Decode
是否总是为你的.png文件提供*image.RGBA
,那么接口类型是更可取的选择。
img.(*image.RGBA).Set(0, 0, color.RGBA{136, 0, 21, 255})
或者
type Setter interface {
Set(x, y int, c color.Color)
}
img.(Setter).Set(0, 0, color.RGBA{136, 0, 21, 255})
或者(可能是最好的选择):
import "image/draw"
...
img.(draw.Image).Set(0, 0, color.RGBA{136, 0, 21, 255})
英文:
reflect.TypeOf(img)
gives you the reflection type of the value in the interface img
, if its an interface. In this case, img
is an interface, an image.Image
which contains an *image.RGBA
.
You can fix your code by converting img
to an *image.RGBA
, or more robustly, define an interface type with the right Set
method, and convert img
to that (the draw.Image
interface in "image/draw"
works perfectly for this, as noted by @DaveC). The interface type is preferable if you aren't sure that png.Decode
will always give you an *image.RGBA
for the .png files you have.
img.(*image.RGBA).Set(0, 0, color.RGBA{136, 0, 21, 255})
or
type Setter interface {
Set(x, y int, c color.Color)
}
img.(Setter).Set(0, 0, color.RGBA{136, 0, 21, 255})
or (probably best):
import "image/draw"
...
img.(draw.Image).Set(0, 0, color.RGBA{136, 0, 21, 255})
答案3
得分: -1
编译器是正确的,image.Image 类型是一个不包含 Set() 函数的接口。
我不是一个图像库的专家,但是我对类型的初步了解似乎表明,你可以使用 Bounds()
方法获取一个 image.Rectangle,然后像你之前的示例代码中那样创建一个新的 RGBA 类型。
// 你当前的图像处理
m := image.NewRGBA(image.Rect(0, 0, 640, 480))
fmt.Println("type:", reflect.TypeOf(m))
m.Set(5, 5, color.RGBA{255, 0, 0, 255})
// 你可以通过传递 image.Image.Bounds() 返回的 image.Rectangle 来创建一个 image.RGBA 类型
m = image.NewRGBA(img.Bounds())
m.Set(0, 0, color.RGBA{136, 0, 21, 255})
这是对你类型问题的严格回答,但我不能保证它能实现你的最终目标。
英文:
The compiler is correct, the image.Image type is an interface that does not include the Set() function.
I am not an expert at the image library but my cursory look at the types seems to suggest you can take an Image type and use the Bounds()
method to get a image.Rectangle to create a new RGBA type as done previously in your example code.
// Your current image manipulation
m := image.NewRGBA(image.Rect(0, 0, 640, 480))
fmt.Println("type:", reflect.TypeOf(m))
m.Set(5, 5, color.RGBA{255, 0, 0, 255})
// You can create a image.RGBA type by passing the image.Rectangle
// returned from image.Image.Bounds()
m = image.NewRGBA(img.Bounds())
m.Set(0, 0, color.RGBA{136, 0, 21, 255})
This is a strict answer to your type issue but I don't have any gurantee it accomplishes your end goals.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论