Golang image.gif.EncodeAll, GIF and DecodeAll example

package image/gif

Golang image.gif.EncodeAll, GIF and DecodeAll usage example

 package main

 import (
 "fmt"
 "image/gif"
 "os"
 )

 func main() {
 inputFile, err := os.Open("input.gif")
 if err != nil {
 fmt.Println(err)
 os.Exit(1)
 }

 defer inputFile.Close()

 g, err := gif.DecodeAll(inputFile)

 if err != nil {
 fmt.Println(err)
 os.Exit(1)
 }

 fmt.Println("Loop count : ", g.LoopCount)
 fmt.Println("Delay : ", g.Delay[:])
 fmt.Println("Image : ", g.Image[:])

 outputFile, err := os.Create("output.gif")

 if err != nil {
 fmt.Println(err)
 os.Exit(1)
 }

 defer outputFile.Close()

 // ok, write out the data into the new GIF file

 err = gif.EncodeAll(outputFile, g)
 if err != nil {
 fmt.Println(err)
 os.Exit(1)
 }

 fmt.Println("Generated image to output.gif \n")
 }

Output(sample) :

Loop count : 0

Delay : [11 11 11 11 11 11 11 11 11 11 11 11 11 11]

Image : [0x208214060 0x208214120 0x2082141e0 0x2082142a0 0x208214360 0x208214420 0x2082144e0 0x2082145a0 0x208214660 0x208214720 0x2082147e0 0x2082148a0 0x208214960 0x208214a20]

Generated image to output.gif

References :

http://golang.org/pkg/image/gif/#EncodeAll

http://golang.org/pkg/image/gif/#GIF

http://golang.org/pkg/image/gif/#DecodeAll

Advertisement