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
+8.3k Swift : Convert (cast) Character to Integer?
+9.5k Golang : Accessing content anonymously with Tor
+7.3k Golang : Calculate how many weeks left to go in a given year
+7.8k Golang : Scan files for certain pattern and rename part of the files
+12.1k Golang : Save webcamera frames to video file
+28.5k Golang : Change a file last modified date and time
+11.7k Golang : How to detect a server/machine network interface capabilities?
+40.5k Golang : Convert to io.ReadSeeker type
+12.7k Android Studio : Highlight ImageButton when pressed on example
+13.2k Golang : How to calculate the distance between two coordinates using Haversine formula
+13.9k Golang : Get dimension(width and height) of image file
+29.5k Golang : How to create new XML file ?