Golang image.png.Encode function example
package image/png
Golang image.png.Encode function usage example
package main
import (
"flag"
"fmt"
"image"
"image/color"
"image/draw"
"image/png"
"math/rand"
"os"
"time"
)
func main() {
flag.Parse()
rand.Seed(time.Now().UTC().UnixNano())
out, err := os.Create("./output.png")
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)
}
}
// ok, write out the data into the new PNG file
err = png.Encode(out, img)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Generated image to output.png \n")
}
Reference :
Advertisement
Something interesting
Tutorials
+11.3k Golang : Post data with url.Values{}
+26.9k Golang : Force your program to run with root permissions
+18.7k Golang : Implement getters and setters
+14.1k Javascript : Prompt confirmation before exit
+8.8k Golang : Take screen shot of browser with JQuery example
+6.3k Golang : Detect face in uploaded photo like GPlus
+5k Golang : Get a list of crosses(instruments) available to trade from Oanda account
+29.5k Golang : Saving(serializing) and reading file with GOB
+7.9k Javascript : Put image into Chrome browser's console
+15k Golang : package is not in GOROOT during compilation
+7.1k Golang : Gorrila mux.Vars() function example
+8.8k Golang : Get final balance from bit coin address example