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
+9.6k Golang : Copy map(hash table) example
+14.3k Golang : How to shuffle elements in array or slice?
+17.9k Golang : How to make a file read only and set it to writable again?
+13.2k Golang : Skip blank/empty lines in CSV file and trim whitespaces example
+12.6k Golang : Transform comma separated string to slice example
+19.9k Golang : How to get time from unix nano example
+7k Golang : Levenshtein distance example
+29.3k Golang : Save map/struct to JSON or XML file
+10.9k Golang : Get UDP client IP address and differentiate clients by port number
+8.2k Golang : Add build version and other information in executables
+24k Golang : Call function from another package
+31.9k Golang : Convert an image file to []byte