Golang 的图片出来通过提供操作每一个像素点设置颜色(http://www.cnblogs.com/ghj1976/p/3441536.html) 和 提供通过可选蒙版图片重叠操作 (http://www.cnblogs.com/ghj1976/p/3443638.html) 这两种基础方式,这样任何想要的效果都可以自己实现, 但是旋转、缩放等相关的图像算法也是比较麻烦的,这时候我们就需要借助官方提供的图片包处理了,图片包在:https://code.google.com/p/graphics-go
获取方法: go get code.google.com/p/graphics-go/graphics
它支持的几个效果举例:
图片旋转
效果:一个旋转前,一个旋转后
代码例子:
1: package main
2:
3: import (
4: "code.google.com/p/graphics-go/graphics"
5: "fmt"
6: "image"
7: "image/png"
8: "log"
9: "os"
10: )
11:
12: func main() {
13: src, err := LoadImage("348.png")
14: if err != nil {
15: log.Fatal(err)
16: }
17:
18: dst := image.NewRGBA(image.Rect(0, 0, 350, 400))
19:
20: err = graphics.Rotate(dst, src, &graphics.RotateOptions{3.5})
21: if err != nil {
22: log.Fatal(err)
23: }
24:
25: // 需要保存的文件
26: imgcounter := 123
27: saveImage(fmt.Sprintf("%03d.png", imgcounter), dst)
28: }
29:
30: // LoadImage decodes an image from a file.
31: func LoadImage(path string) (img image.Image, err error) {
32: file, err := os.Open(path)
33: if err != nil {
34: return
35: }
36: defer file.Close()
37: img, _, err = image.Decode(file)
38: return
39: }
40:
41: // 保存Png图片
42: func saveImage(path string, img image.Image) (err error) {
43: // 需要保存的文件
44: imgfile, err := os.Create(path)
45: defer imgfile.Close()
46:
47: // 以PNG格式保存文件
48: err = png.Encode(imgfile, img)
49: if err != nil {
50: log.Fatal(err)
51: }
52: return
53: }
代码说明:
旋转的参数是顺时针旋转的弧度,弧度相关的介绍如下:
http://youthpasses.blog.51cto.com/2909834/799353
每个角度对应的弧度可以看下面图。
代码参考:http://stackoverflow.com/questions/12430874/image-manipulation-in-golang
图片模糊处理
效果:
下面一个是清晰版本,一个是模糊出来后的版本。
仔细对比细节是可以看到模糊效果的。
代码:
1: package main
2:
3: import (
4: "code.google.com/p/graphics-go/graphics"
5: "fmt"
6: "image"
7: "image/png"
8: "log"
9: "os"
10: )
11:
12: func main() {
13: src, err := LoadImage("348.png")
14: if err != nil {
15: log.Fatal(err)
16: }
17:
18: dst := image.NewRGBA(image.Rect(0, 0, 350, 400))
19:
20: err = graphics.Blur(dst, src, &graphics.BlurOptions{StdDev: 1.1})
21: if err != nil {
22: log.Fatal(err)
23: }
24:
25: // 需要保存的文件
26: imgcounter := 510
27: saveImage(fmt.Sprintf("%03d.png", imgcounter), dst)
28: }
29:
30: // LoadImage decodes an image from a file.
31: func LoadImage(path string) (img image.Image, err error) {
32: file, err := os.Open(path)
33: if err != nil {
34: return
35: }
36: defer file.Close()
37: img, _, err = image.Decode(file)
38: return
39: }
40:
41: // 保存Png图片
42: func saveImage(path string, img image.Image) (err error) {
43: // 需要保存的文件
44: imgfile, err := os.Create(path)
45: defer imgfile.Close()
46:
47: // 以PNG格式保存文件
48: err = png.Encode(imgfile, img)
49: if err != nil {
50: log.Fatal(err)
51: }
52: return
53: }
代码说明:
模糊参数:
1: // BlurOptions are the blurring parameters.
2: // StdDev is the standard deviation of the normal, higher is blurrier.
3: // StdDev 是正常的标准偏差, 值越大越虚化
4: // Size is the size of the kernel. If zero, it is set to Ceil(6 * StdDev).
5: //
6: type BlurOptions struct {
7: StdDev float64
8: Size int
9: }
缩略图
原始图:
确保数据完整的缩放,效果如下:
相关代码:
1: package main
2:
3: import (
4: "code.google.com/p/graphics-go/graphics"
5: "fmt"
6: "image"
7: "image/png"
8: "log"
9: "os"
10: )
11:
12: func main() {
13: src, err := LoadImage("348.png")
14: if err != nil {
15: log.Fatal(err)
16: }
17:
18: // 缩略图的大小
19: dst := image.NewRGBA(image.Rect(0, 0, 20, 80))
20:
21: // 产生缩略图,等比例缩放
22: err = graphics.Scale(dst, src)
23: if err != nil {
24: log.Fatal(err)
25: }
26:
27: // 需要保存的文件
28: imgcounter := 734
29: saveImage(fmt.Sprintf("%03d.png", imgcounter), dst)
30: }
31:
32: // LoadImage decodes an image from a file.
33: func LoadImage(path string) (img image.Image, err error) {
34: file, err := os.Open(path)
35: if err != nil {
36: return
37: }
38: defer file.Close()
39: img, _, err = image.Decode(file)
40: return
41: }
42:
43: // 保存Png图片
44: func saveImage(path string, img image.Image) (err error) {
45: // 需要保存的文件
46: imgfile, err := os.Create(path)
47: defer imgfile.Close()
48:
49: // 以PNG格式保存文件
50: err = png.Encode(imgfile, img)
51: if err != nil {
52: log.Fatal(err)
53: }
54: return
55: }
图片数据可以丢弃的缩放效果:
相关代码:
1: package main
2:
3: import (
4: "code.google.com/p/graphics-go/graphics"
5: "fmt"
6: "image"
7: "image/png"
8: "log"
9: "os"
10: )
11:
12: func main() {
13: src, err := LoadImage("348.png")
14: if err != nil {
15: log.Fatal(err)
16: }
17:
18: // 缩略图的大小
19: dst := image.NewRGBA(image.Rect(0, 0, 20, 80))
20:
21: // 产生缩略图
22: err = graphics.Thumbnail(dst, src)
23: if err != nil {
24: log.Fatal(err)
25: }
26:
27: // 需要保存的文件
28: imgcounter := 670
29: saveImage(fmt.Sprintf("%03d.png", imgcounter), dst)
30: }
31:
32: // LoadImage decodes an image from a file.
33: func LoadImage(path string) (img image.Image, err error) {
34: file, err := os.Open(path)
35: if err != nil {
36: return
37: }
38: defer file.Close()
39: img, _, err = image.Decode(file)
40: return
41: }
42:
43: // 保存Png图片
44: func saveImage(path string, img image.Image) (err error) {
45: // 需要保存的文件
46: imgfile, err := os.Create(path)
47: defer imgfile.Close()
48:
49: // 以PNG格式保存文件
50: err = png.Encode(imgfile, img)
51: if err != nil {
52: log.Fatal(err)
53: }
54: return
55: }
更多相关资料请看下面地址: