Golang image.png.Decode function example
package image/png
Golang image.png.Decode 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
+18.5k Golang : Set, Get and List environment variables
+21.2k Golang : Convert(cast) string to rune and back to string example
+13.4k Golang : Verify token from Google Authenticator App
+11.7k Golang : How to detect a server/machine network interface capabilities?
+10.6k RPM : error: db3 error(-30974) from dbenv->failchk: DB_RUNRECOVERY: Fatal error, run database recovery
+17.4k Golang : Check if IP address is version 4 or 6
+8.3k Golang : Emulate NumPy way of creating matrix example
+5.9k Golang : Denco multiplexer example
+8.7k Golang : Find duplicate files with filepath.Walk
+11.6k Golang : Simple file scaning and remove virus example
+10.1k Golang : How to tokenize source code with text/scanner package?