Golang官方圖片庫

Golang 的圖片出來經過提供操做每個像素點設置顏色(http://www.cnblogs.com/ghj1976/p/3441536.html) 和 提供經過可選蒙版圖片重疊操做 (http://www.cnblogs.com/ghj1976/p/3443638.html) 這兩種基礎方式,這樣任何想要的效果均可以本身實現, 可是旋轉、縮放等相關的圖像算法也是比較麻煩的,這時候咱們就須要藉助官方提供的圖片包處理了,圖片包在:https://code.google.com/p/graphics-gohtml

獲取方法: go get code.google.com/p/graphics-go/graphicsgolang

它支持的幾個效果舉例:算法

 

圖片旋轉

效果:一個旋轉前,一個旋轉後app

348 123

代碼例子:google

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

代碼說明:spa

旋轉的參數是順時針旋轉的弧度,弧度相關的介紹以下:3d

http://youthpasses.blog.51cto.com/2909834/799353code

每一個角度對應的弧度能夠看下面圖。orm

144218298

代碼參考:http://stackoverflow.com/questions/12430874/image-manipulation-in-golanghtm

圖片模糊處理

效果:

下面一個是清晰版本,一個是模糊出來後的版本。

348 510

仔細對比細節是能夠看到模糊效果的。

代碼:

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

縮略圖

原始圖:

348

確保數據完整的縮放,效果以下:

734

相關代碼:

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

圖片數據能夠丟棄的縮放效果:

670

 

相關代碼:

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

 

更多相關資料請看下面地址:

https://code.google.com/p/graphics-go/source/browse/graphics/?r=9a6eb915f43de825cd2a26c8b8866422d0a3f2ec

相關文章
相關標籤/搜索