生命不止,繼續 go go go~~~~
今天做一點圖片處理的東西,跟大家一起學習分享。
很久之前,介紹過golang提供的關於圖片的標準庫:Go語言學習之image、image/color、image/png、image/jpeg包(the way to go)
當你search on google或百度一下的時候,你會發現很多提到了graphics-go/graphics,但是這個庫不知道為何,官方好像不再提供了,很難找到了。
那也沒關係,我們還有面向github程式設計呢!!!
溫故而知新,先看一下之前的程式碼:生成圖片
package mainimport "image"import "image/color"import "image/png"import "os"func main() { // Create an 100 x 50 image img := image.NewRGBA(image.Rect(0, 0, 100, 50)) // Draw a red dot at (2, 3) img.Set(2, 3, color.RGBA{255, 0, 0, 255}) // Save to out.png f, _ := os.OpenFile("out.png", os.O_WRONLY|os.O_CREATE, 0600) defer f.Close() png.Encode(f, img)}12345678910111213141516171819
disintegration/imaginggithub地址:https://github.com/disintegration/imaging
Star: 1284
獲取:go get -u github.com/disintegration/imaging
生成縮圖下面的程式碼,將三張圖片生成一張縮圖。出自:https://www.socketloop.com/tutorials/golang-generate-thumbnails-from-images
package mainimport ( "image" "image/color" "runtime" "github.com/disintegration/imaging")func main() { // use all CPU cores for maximum performance runtime.GOMAXPROCS(runtime.NumCPU()) // input files files := []string{"1.jpg", "2.jpg", "3.jpg"} // load images and make 100x100 thumbnails of them var thumbnails []image.Image for _, file := range files { img, err := imaging.Open(file) if err != nil { panic(err) } thumb := imaging.Thumbnail(img, 100, 100, imaging.CatmullRom) thumbnails = append(thumbnails, thumb) } // create a new blank image dst := imaging.New(100*len(thumbnails), 100, color.NRGBA{0, 0, 0, 0}) // paste thumbnails into the new image side by side for i, thumb := range thumbnails { dst = imaging.Paste(dst, thumb, image.Pt(i*100, 0)) } // save the combined image to file err := imaging.Save(dst, "dst.jpg") if err != nil { panic(err) }}123456789101112131415161718192021222324252627282930313233343536373839404142
生成縮圖服務接下來,根據使用者提供的url,生成不同尺寸的縮圖。
關於golang中net/http可以參考:Go語言學習之net/http包(The way to go)
package mainimport ( "fmt" "image" "image/color" "image/png" "net/http" "runtime" "strconv" "strings" "github.com/disintegration/imaging")func main() { // use all CPU cores for maximum performance runtime.GOMAXPROCS(runtime.NumCPU()) http.HandleFunc("/", doImageHandler) http.ListenAndServe("localhost:8080", nil)}func doImageHandler(w http.ResponseWriter, r *http.Request) { fmt.Printf("%q\n", strings.Split(r.URL.Path, "/")) url := strings.Split(r.URL.Path, "/") if len(url) != 3 { return } thumbnails_size := strings.Split(url[2], "_") if len(thumbnails_size) != 2 { return } thumbnails_width, _ := strconv.Atoi(thumbnails_size[0]) thumbnails_height, _ := strconv.Atoi(thumbnails_size[1]) img, err := imaging.Open(url[1]) if err != nil { panic(err) } thumb := imaging.Thumbnail(img, thumbnails_width, thumbnails_height, imaging.CatmullRom) dst := imaging.New(thumbnails_width, thumbnails_height, color.NRGBA{0, 0, 0, 0}) dst = imaging.Paste(dst, thumb, image.Pt(0, 0)) if err != nil { panic(err) } header := w.Header() header.Add("Content-Type", "image/jpeg") png.Encode(w, dst)}12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
例如,瀏覽器輸入:http://localhost:8080/1.jpg/80_90就會生成一張80*90的1.jpg的縮圖
nfnt/resizegithub地址:https://github.com/nfnt/resize
Star: 1544
獲取:go get github.com/nfnt/resize
等比例放大縮小圖片
package mainimport ( "image/jpeg" "log" "os" "github.com/nfnt/resize")func main() { file, err := os.Open("1.jpg") if err != nil { log.Fatal(err) } img, err := jpeg.Decode(file) if err != nil { log.Fatal(err) } file.Close() // resize to width 1000 using Lanczos resampling // and preserve aspect ratio m := resize.Resize(100, 0, img, resize.Lanczos3) out, err := os.Create("test_resized.jpg") if err != nil { log.Fatal(err) } defer out.Close() // write new image to file jpeg.Encode(out, m, nil)}1234567891011121314151617181920212223242526272829303132333435
最新評論