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.1k Golang : Get curl -I or head data from URL example
+38.1k Golang : Read a text file and replace certain words
+9.9k Golang : ffmpeg with os/exec.Command() returns non-zero status
+5.2k Golang : PGX CopyFrom to insert rows into Postgres database
+8.9k Golang : Accept any number of function arguments with three dots(...)
+5.9k Golang : Detect variable or constant type
+14.8k Golang : Get URI segments by number and assign as variable example
+5.8k CodeIgniter/PHP : Remove empty lines above RSS or ATOM xml tag
+28.2k Golang : Connect to database (MySQL/MariaDB) server
+19.6k Golang : Close channel after ticker stopped example
+25.8k Golang : Daemonizing a simple web server process example
+17.3k Golang : How to tell if a file is compressed either gzip or zip ?