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.4k Golang : How to protect your source code from client, hosting company or hacker?
+15.2k Golang : How to check if IP address is in range
+7.3k Golang : alternative to os.Exit() function
+7.5k Golang : Handling Yes No Quit query input
+9.7k PHP : Get coordinates latitude/longitude from string
+22.1k Golang : Join arrays or slices example
+6.1k Golang : Scan forex opportunities by Bollinger bands
+9.4k Golang : Apply Histogram Equalization to color images
+7.5k Golang : Create zip/ePub file without compression(use Store algorithm)
+5.7k Unix/Linux/MacOSx : Get local IP address
+19.3k Golang : Get host name or domain name from IP address
+23.2k Golang : Print out struct values in string format