Golang中的image包提供了基本的圖像類型、顏色模型、以及用于處理圖像的各種函數和接口。
常用類型與接口
image.Image
?接口
這是Go語言中處理圖像的核心接口,定義了所有圖像必須實現的方法:
type Image interface {// Bounds returns the domain for which At can return non-zero color.// The bounds do not necessarily contain the point (0, 0).Bounds() Rectangle// At returns the color of the pixel at (x, y).// At must panic if x or y are outside the bounds of the image.At(x, y int) color.Color
}
color.Color
?接口
表示一種顏色,需要實現以下方法:?
type Color interface {// RGBA returns the alpha-premultiplied red, green, blue and alpha values// for the color. Each value ranges within [0, 0xffff], but is represented// by a uint32 so that multiplying by a blend factor up to 0xffff will not// overflow.RGBA() (r, g, b, a uint32)
}
color.RGBA
?結構體
實現了color.Color
接口,代表一個由紅綠藍透明度組成的顏色:?
type RGBA struct {R, G, B, A uint8
}
image.Decode
從輸入流(如文件或網絡連接)解碼圖像,并返回一個實現了image.Image接口的對象:
func Decode(r io.Reader) (img image.Image, err error)
file, err := os.Open("example.png")
if err != nil {panic(err)
}
defer file.Close()img, _, err := image.Decode(file)
if err != nil {panic(err)
}
// 使用解碼后的img進行后續操作
image.DecodeConfig
僅解碼圖像的配置信息而不加載完整圖像數據:?
func DecodeConfig(r io.Reader) (cfg image.Config, err error)
file, err := os.Open("example.gif")
if err != nil {panic(err)
}
defer file.Close()config, err := image.DecodeConfig(file)
if err != nil {panic(err)
}
fmt.Printf("Image dimensions: %d x %d, Color model: %v\n", config.Width, config.Height, config.ColorModel)
案例
package mainimport ("image""image/draw""image/jpeg""os"
)func main() {// 讀取原圖file, err := os.Open("需要時jpeg格式的才行.jpeg")if err != nil {panic(err)}defer file.Close()img, err := jpeg.Decode(file)if err != nil {panic(err)}width := 600height := 400// 創建一個新的圖片,大小為指定的寬和高newImg := image.NewRGBA(image.Rect(0, 0, width, height))// 裁剪圖片 使用 draw.Draw 簡單縮放(質量較低,可能會出現像素化)draw.Draw(newImg, newImg.Bounds(), img, image.Point{}, draw.Src)// 重新編碼并保存outputFile, err := os.Create("output.jpg")if err != nil {panic(err)}defer outputFile.Close()// 設置壓縮選項outputQuality := 80opts := &jpeg.Options{Quality: outputQuality,}err = jpeg.Encode(outputFile, newImg, opts)if err != nil {panic(err)}
}
?使用?draw.Src
?方式將原圖直接繪制到目標圖像上,這相當于最簡單的像素復制,可能會導致圖像質量下降,特別是對于縮小操作時,會出現明顯的像素化現象。這種方法適用于對圖像質量要求不高的場景,或者作為臨時解決方案
請注意,這種方法并不推薦用于高質量的圖像縮放,因為它沒有采用任何插值算法來平滑過渡像素,導致縮放后的圖像質量較差。對于實際項目中對圖像大小調整的需求,建議使用專門的圖像處理庫如?github.com/nfnt/resize
它提供了多種高質量的插值算法(如 Lanczos 等),能夠更好地保持圖像細節和視覺效果。
這些只是image
包中的一部分功能。根據實際需求,還可以使用其他子包(如image/jpeg
,?image/png
,?image/gif
等)進行特定格式的編碼和解碼,或利用image/draw
包進行更復雜的圖像合成操作。
使用第三方包處理
imaging一個簡單、實用的圖像處理工具
文檔:
-
https://github.com/disintegration/imaging
-
https://pkg.go.dev/github.com/disintegration/imaging
-
Go Image Filtering Toolkit: https://github.com/disintegration/gift
安裝
????????go?get?github.com/disintegration/imaging?
package mainimport ("github.com/disintegration/imaging""log"
)func main() {// 打開一個圖片文件src, err := imaging.Open("./1716282750475.jpg")if err != nil {log.Fatalf("無法打開圖像: %v", err)}// 生成縮略圖dst := imaging.Thumbnail(src, 100, 100, imaging.Lanczos)// 保存err = imaging.Save(dst, "thumbnail.jpg")if err != nil {log.Fatalf("無法保存圖像: %v", err)} else {log.Println("保存圖像成功")}
}