英文:
Why I'm getting segmentation violation error in Go
问题
我正在尝试导入两个PNG图像,并将一个粘贴在另一个上面。输出将为JPEG格式。
每当我运行下面的代码时,都会出现以下错误:
image: unknown format
image: unknown format
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x109d7a4]
goroutine 1 [running]:
main.main()
只要使用"Bounds()"方法,就会出现错误。你认为是什么原因导致的呢?
代码:
func main() {
imgFile1, err := os.Open("./pics/00/body.png")
if err != nil {
fmt.Println(err)
}
defer imgFile1.Close()
imgFile2, err := os.Open("./pics/01/face.png")
if err != nil {
fmt.Println(err)
}
defer imgFile2.Close()
img1, _, err := image.Decode(imgFile1)
if err != nil {
fmt.Println(err)
}
img2, _, err := image.Decode(imgFile2)
if err != nil {
fmt.Println(err)
}
//第二个图像的新矩形
sp1 := image.Point{0,0}
sp2 := image.Point{img1.Bounds().Dx(),img1.Bounds().Dy()}
r2 := image.Rectangle{sp1, sp2}
//创建新图像
rgba := image.NewRGBA(r2)
//将两个图像绘制到这个新图像中
draw.Draw(rgba, r2, img1, image.Point{0, 0}, draw.Src)
draw.Draw(rgba, r2, img2, image.Point{0, 0}, draw.Src)
//导出图像
out, err := os.Create("./output.jpg")
if err != nil {
fmt.Println(err)
}
defer out.Close()
var opt jpeg.Options
opt.Quality = 80
jpeg.Encode(out, rgba, &opt)
}
英文:
I'm trying to import 2 png image and paste one on top of the other. The output will be jpeg.
When ever I run the code below, I got this error:
image: unknown format
image: unknown format
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x109d7a4]
goroutine 1 [running]:
main.main()
Error is coming whenever I use "Bounds()" method. What do you think is the cause.
code:
func main() {
imgFile1, err := os.Open("./pics/00/body.png")
if err != nil {
fmt.Println(err)
}
defer imgFile1.Close()
imgFile2, err := os.Open("./pics/01/face.png")
if err != nil {
fmt.Println(err)
}
defer imgFile2.Close()
img1, _, err := image.Decode(imgFile1)
if err != nil {
fmt.Println(err)
}
img2, _, err := image.Decode(imgFile2)
if err != nil {
fmt.Println(err)
}
//new rectangle for the second image
sp1 := image.Point{0,0}
sp2 := image.Point{img1.Bounds().Dx(),img1.Bounds().Dy()}
r2 := image.Rectangle{sp1, sp2}
//create new image
rgba := image.NewRGBA(r2)
//draw the two images into this new image
draw.Draw(rgba, r2, img1, image.Point{0, 0}, draw.Src)
draw.Draw(rgba, r2, img2, image.Point{0, 0}, draw.Src)
//export it
out, err := os.Create("./output.jpg")
if err != nil {
fmt.Println(err)
}
defer out.Close()
var opt jpeg.Options
opt.Quality = 80
jpeg.Encode(out, rgba, &opt)
}
答案1
得分: 2
我不确定为什么Go语言有两种这样的解码方式。如果你知道文件类型,我认为最好只使用特定的包:
package main
import (
"fmt"
"image/png"
"net/http"
)
const ava = "http://gravatar.com/avatar/5ec9c21c8d54825b04def7a41998d18d"
func main() {
r, err := http.Get(ava)
if err != nil {
panic(err)
}
defer r.Body.Close()
i, err := png.Decode(r.Body)
if err != nil {
panic(err)
}
dx := i.Bounds().Dx()
fmt.Println(dx)
}
如果你坚持使用另一种方法,你需要添加一个导入(感谢mkopriva):
import (
"image"
_ "image/png"
)
英文:
I'm not sure why Go has two ways to decode like this. If you know the file type, I think it's better to just use the specific package:
package main
import (
"fmt"
"image/png"
"net/http"
)
const ava = "http://gravatar.com/avatar/5ec9c21c8d54825b04def7a41998d18d"
func main() {
r, err := http.Get(ava)
if err != nil {
panic(err)
}
defer r.Body.Close()
i, err := png.Decode(r.Body)
if err != nil {
panic(err)
}
dx := i.Bounds().Dx()
fmt.Println(dx)
}
If you insist on using the other method, you will need to add an import (thanks to mkopriva):
import (
"image"
_ "image/png"
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论