Golang图片库gg使用示例
示例一代码如下
package mainimport ("fmt""github.com/fogleman/gg"
)func main() {const S = 1024//NewContext调整图片大小dc := gg.NewContext(S, S)//图片颜色,三个参数依次为红绿蓝,第四个参数表示透明度dc.SetRGBA255(255, 0, 0,30)for i := 0; i < 360; i += 15 {dc.Push()dc.RotateAbout(gg.Radians(float64(i)), S/2, S/2)//DrawEllipse画椭圆dc.DrawEllipse(S/2, S/2, S*7/16, S/8)//DrawRectangle画矩形//dc.DrawRectangle(S/2, S/2, S*7/16, S/8)//DrawCircle画圆//dc.DrawCircle(650,650,130)dc.Fill()dc.Pop()}err := dc.SavePNG("../../Desktop/ellipse2.png")if err != nil {fmt.Println(err)}
}
合并图片
代码如下
package mainimport ("fmt""github.com/fogleman/gg"
)func main() {const S = 1024//NewContext调整图片大小dc := gg.NewContext(S, S)//图片颜色,三个参数依次为红绿蓝,第四个参数表示透明度dc.SetRGBA255(255, 0, 0,30)for i := 0; i < 360; i += 15 {dc.Push()dc.RotateAbout(gg.Radians(float64(i)), S/2, S/2)dc.DrawEllipse(S/2, S/2, S*7/16, S/8)dc.Fill()dc.Pop()}//加载图片image, err := gg.LoadPNG("../../Desktop/gopher.png")if err != nil {fmt.Println(err)}//画图片,后两个参数为x,y轴坐标dc.DrawImage(image,425,380)err = dc.SavePNG("../../Desktop/out.png")if err != nil {fmt.Println(err)}
}