golang中image/gif包用法

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

gif包實現了gif圖片的解碼及編碼

func Decode(r io.Reader) (image.Image, error)      //Decode從r中讀取一個GIF映像,然後返回的image.Image是第一個嵌入的圖。


func DecodeConfig(r io.Reader) (image.Config, error)   //DecodeConfig不需要解碼整個映像就可以返回全域的顏色模型和GIF圖片的尺寸。

type Config struct {    ColorModel    color.Model    Width, Height int}
Config返回映像的顏色model和尺寸

func Encode(w io.Writer, m image.Image, o *Options) error    //將圖片m按照gif模式寫入w中

type Options struct {// NumColors是圖片中使用顏色的最大值,它的範圍是1-256NumColors int// Quantizer經常被用來通過NumColors產生調色盤,palette.Plan9 被用來替代nil QuantizerQuantizer draw.Quantizer// Drawer i用於將源圖片轉化為期望的調色盤, draw.FloydSteinberg 用來替代一個空 Drawer.Drawer draw.Drawer}


func EncodeAll(w io.Writer, g *GIF) error    //將圖片按照幀與幀之間指定的迴圈次數和時延寫入w中

type GIF struct {    Image     []*image.Paletted // 連續的圖片    Delay     []int             // 連續的延遲時間,每一幀單位都是百分之一秒,delay中數值表示其兩個映像動態展示的時間間隔    LoopCount int               // 迴圈次數,如果為0則一直迴圈。}

func DecodeAll(r io.Reader) (*GIF, error) //DecodeAll 從r上讀取一個GIF圖片,並且返回順序的幀和時間資訊

簡單舉例說明如何利用gif包製作一個gif圖片,畫兩條垂直相交的動態圖線(如果想要得到複雜的gif映像,可以自己設定較複雜的畫線以及顏色模式,得到複雜圖形):

package mainimport ("fmt""image""image/color""image/color/palette""image/gif""net/http""os")func main() {http.HandleFunc("/display", display)err := http.ListenAndServe(":9100", nil)if err != nil {fmt.Println(err)}}func display(w http.ResponseWriter, q *http.Request) {f1, err := os.Create("test.gif")if err != nil {fmt.Println(err)}defer f1.Close()p1 := image.NewPaletted(image.Rect(0, 0, 110, 110), palette.Plan9)for x := 0; x < 100; x++ {for y := 0; y < 100; y++ {p1.Set(50, y, color.RGBA{uint8(x), uint8(y), 255, 255})}}p2 := image.NewPaletted(image.Rect(0, 0, 210, 210), palette.Plan9)for x := 0; x < 100; x++ {for y := 0; y < 100; y++ {p2.Set(x, 50, color.RGBA{uint8(x * x % 255), uint8(y * y % 255), 0, 255})}}g1 := &gif.GIF{Image:     []*image.Paletted{p1, p2},Delay:     []int{30, 30},LoopCount: 0,}gif.EncodeAll(w, g1)  //瀏覽器顯示gif.EncodeAll(f1, g1) //儲存到檔案中}

動態gif映像如下:


當然,也可以利用已有圖片產生gif,代碼如下:

package mainimport ("fmt""image""image/color/palette""image/draw""image/gif""image/jpeg""image/png""net/http""os")func main() {http.HandleFunc("/display", display)err := http.ListenAndServe(":9100", nil)if err != nil {fmt.Println(err)}}func display(w http.ResponseWriter, q *http.Request) {f, err := os.Open("test.jpeg")if err != nil {fmt.Println(err)}defer f.Close()g, err := jpeg.Decode(f)if err != nil {fmt.Println(err)}f2, err := os.Open("123.png")if err != nil {fmt.Println(err)}defer f.Close()g2, err := png.Decode(f2)if err != nil {fmt.Println(err)}f1, err := os.Create("test.gif")if err != nil {fmt.Println(err)}defer f1.Close()p1 := image.NewPaletted(image.Rect(0, 0, 200, 200), palette.Plan9)draw.Draw(p1, p1.Bounds(), g, image.ZP, draw.Src) //添加圖片p2 := image.NewPaletted(image.Rect(0, 0, 200, 200), palette.Plan9)draw.Draw(p2, p2.Bounds(), g2, image.ZP, draw.Src) //添加圖片g1 := &gif.GIF{Image:     []*image.Paletted{p1, p2},Delay:     []int{30, 30},LoopCount: 0,}gif.EncodeAll(w, g1)  gif.EncodeAll(f1, g1) }
得到的gif圖片如下所示:


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.