在Golang中进行图像处理 [英] Image manipulation in Golang

查看:497
本文介绍了在Golang中进行图像处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




  1. 背景图片(bi)

  2. Image1(i1)

  3. Image3(i2)

我想将i1和i2置于bi的某个角度然后生成最终图像。我有i1和i2的x和y轴值以及它们的预期旋转角度。 i1和i2可能部分重叠在一起。但我有i1和i2的z索引要知道,如果万一他们重叠,那么谁会在前台。



我正在尝试在Golang中实现这一点。 >
http://golang.org/doc/articles/image_draw.html似乎这样做。任何人都知道任何类似的代码示例,这可能会有帮助。或者你能告诉我Golang的几行是伪程序吗?



谢谢。

解决方案

不确定你在找什么,我还没有用过很多的图像包......但只是遵循示例代码并使用graphics-go 包(它适用于我),我至少可以做一些事情。

 









$ b $ image
image / jpeg
code.google.com/p/graphics-go/graphics


func main(){
fImg1,_:= os.Open(arrow1.jpg)
推迟fImg1.Close()
img1,_,_:= image.Decode(fImg1)

fImg2,_:= os.Open(arrow2.jpg)
推迟fImg2.Close()
img2,_,_:= image.Decode(fImg2)

m:= image.NewRGBA(image.Rect(0,0,800,600))
draw.Draw(m,m.Bounds(),img1,image.Poi nt {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})
}


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.

解决方案

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})
}

这篇关于在Golang中进行图像处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆