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
+6.1k Java : Human readable password generator
+9.7k Random number generation with crypto/rand in Go
+13.5k Golang : How to get year, month and day?
+13.4k Golang : error parsing regexp: invalid or unsupported Perl syntax
+5.4k Golang : fmt.Println prints out empty data from struct
+18.5k Golang : Example for RSA package functions
+8.8k Android Studio : Image button and button example
+8.8k Golang : Gorilla web tool kit schema example
+18.8k Golang : Implement getters and setters
+19.9k Golang : Count JSON objects and convert to slice/array
+39k Golang : How to iterate over a []string(array)
+19.5k Golang : How to Set or Add Header http.ResponseWriter?