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.3k Apt-get to install and uninstall Golang
+51.1k Golang : Disable security check for HTTPS(SSL) with bad or expired certificate
+39.2k Golang : How to read CSV file
+46.2k Golang : Read tab delimited file with encoding/csv package
+11.5k Golang : Format numbers to nearest thousands such as kilos millions billions and trillions
+7.5k Golang : Detect sample rate, channels or latency with PortAudio
+11.7k Golang : How to detect a server/machine network interface capabilities?
+7.3k Golang : Of hash table and hash map
+11.3k Golang : Characters limiter example
+7.4k Golang : Scanf function weird error in Windows
+4.7k Unix/Linux : How to pipe/save output of a command to file?
+32.1k Golang : Validate email address with regular expression