在Golang中的图像处理

huangapple go评论91阅读模式
英文:

Image manipulation in Golang

问题

我有以下内容:

  1. 背景图像(bi)
  2. 图像1(i1)
  3. 图像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:

  1. Background image (bi)
  2. Image1 (i1)
  3. 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})
}

huangapple
  • 本文由 发表于 2012年9月15日 03:32:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/12430874.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定