Golang image.gif.Encode function example
package image/gif
Golang image.gif.Encode function usage example
package main
import (
"flag"
"fmt"
"image"
"image/color"
"image/draw"
"image/gif"
"math/rand"
"os"
"time"
)
func main() {
flag.Parse()
rand.Seed(time.Now().UTC().UnixNano())
out, err := os.Create("./output.gif")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
// generate some QR code look a like image
imgRect := image.Rect(0, 0, 100, 100)
img := image.NewGray(imgRect)
draw.Draw(img, img.Bounds(), &image.Uniform{color.White}, image.ZP, draw.Src)
for y := 0; y < 100; y += 10 {
for x := 0; x < 100; x += 10 {
fill := &image.Uniform{color.Black}
if rand.Intn(10)%2 == 0 {
fill = &image.Uniform{color.White}
}
draw.Draw(img, image.Rect(x, y, x+10, y+10), fill, image.ZP, draw.Src)
}
}
var opt gif.Options
opt.NumColors = 256 // you can add more parameters if you want
// ok, write out the data into the new GIF file
err = gif.Encode(out, img, &opt) // put num of colors to 256 <------- here
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Generated image to output.gif \n")
}
Reference :
Advertisement
Something interesting
Tutorials
+16.9k Golang : How to generate QR codes?
+17k Golang : Get number of CPU cores
+13.4k Golang : Verify token from Google Authenticator App
+5.9k Golang : Generate multiplication table from an integer example
+4.8k Which content-type(MIME type) to use for JSON data
+10.5k RPM : error: db3 error(-30974) from dbenv->failchk: DB_RUNRECOVERY: Fatal error, run database recovery
+8.8k Golang : Executing and evaluating nested loop in html template
+18.6k Golang : Iterating Elements Over A List
+10.7k Golang : Underscore string example
+14.3k Golang : How to shuffle elements in array or slice?
+17k Golang : Capture stdout of a child process and act according to the result