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
+12.4k Golang : Search and extract certain XML data example
+5.8k Unix/Linux : How to test user agents blocked successfully ?
+8.2k Golang : Qt splash screen with delay example
+33.8k Golang : convert(cast) bytes to string
+12.2k Golang : Get remaining text such as id or filename after last segment in URL path
+7.1k Golang : A simple forex opportunities scanner
+16.3k Golang : Loop each day of the current month example
+7.8k Golang : Getting Echo framework StartAutoTLS to work
+18.2k Golang : Get command line arguments
+11.4k Golang : Delay or limit HTTP requests example
+12.4k Golang : Encrypt and decrypt data with x509 crypto