英文:
Image manipulation in Golang
问题
我有以下内容:
- 背景图像(bi)
- 图像1(i1)
- 图像2(i2)
我想要将i1和i2以一定的角度放置在bi上,然后生成最终的图像。我有i1和i2的x和y轴值以及它们期望的旋转角度。i1和i2可能部分重叠在彼此上面,但我有i1和i2的z索引来知道,如果它们重叠,那么谁将在前景。
我正在尝试在Golang中实现这个。http://golang.org/doc/articles/image_draw.html似乎可以做到这一点。有人知道任何类似的代码示例,可以帮助我吗?或者你能给我展示几行Golang的伪代码吗?
谢谢。
英文:
I have the following:
- Background image (bi)
- Image1 (i1)
- Image3 (i2)
I want to position i1 and i2 over bi with some angle and then produce a final image. I have x and y axis value for i1 and i2 and their expected rotation angle. i1 and i2 may partially overlay on each other. but I have z index for i1 and i2 to know, if in case they overlap then who will be in foreground.
I am trying to achieve this in Golang.
http://golang.org/doc/articles/image_draw.html seems to do this. Anyone knows any similar example of code, that may help. Or can you show me couple of lines in Golang as a pseudo program?
Thanks.
答案1
得分: 40
package main
import (
"fmt"
"os"
"image/draw"
"image"
"image/jpeg"
"code.google.com/p/graphics-go/graphics"
)
func main() {
fImg1, _ := os.Open("arrow1.jpg")
defer fImg1.Close()
img1, _, _ := image.Decode(fImg1)
fImg2, _ := os.Open("arrow2.jpg")
defer fImg2.Close()
img2, _, _ := image.Decode(fImg2)
m := image.NewRGBA(image.Rect(0, 0, 800, 600))
draw.Draw(m, m.Bounds(), img1, image.Point{0,0}, draw.Src)
//draw.Draw(m, m.Bounds(), img2, image.Point{-200,-200}, draw.Src)
graphics.Rotate(m, img2, &graphics.RotateOptions{3.5})
toimg, _ := os.Create("new.jpg")
defer toimg.Close()
jpeg.Encode(toimg, m, &jpeg.Options{jpeg.DefaultQuality})
}
英文:
Not sure exactly what you are looking for and I haven't worked with the image package much at all ... but just following the sample code and using graphics-go package (it works for me), I was able to do something at least.
package main
import (
"fmt"
"os"
"image/draw"
"image"
"image/jpeg"
"code.google.com/p/graphics-go/graphics"
)
func main() {
fImg1, _ := os.Open("arrow1.jpg")
defer fImg1.Close()
img1, _, _ := image.Decode(fImg1)
fImg2, _ := os.Open("arrow2.jpg")
defer fImg2.Close()
img2, _, _ := image.Decode(fImg2)
m := image.NewRGBA(image.Rect(0, 0, 800, 600))
draw.Draw(m, m.Bounds(), img1, image.Point{0,0}, draw.Src)
//draw.Draw(m, m.Bounds(), img2, image.Point{-200,-200}, draw.Src)
graphics.Rotate(m, img2, &graphics.RotateOptions{3.5})
toimg, _ := os.Create("new.jpg")
defer toimg.Close()
jpeg.Encode(toimg, m, &jpeg.Options{jpeg.DefaultQuality})
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论