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
+21.8k Golang : Upload big file (larger than 100MB) to AWS S3 with multipart upload
+18.1k Golang : Convert IPv4 address to decimal number(base 10) or integer
+6.9k Golang : How to solve "too many .rsrc sections" error?
+11.8k Golang : convert(cast) float to string
+24.5k Golang : GORM read from database example
+29.7k Golang : Record voice(audio) from microphone to .WAV file
+17.9k Golang : Qt image viewer example
+5k Python : Convert(cast) bytes to string example
+8.5k Golang : How to check variable or object type during runtime?
+29.3k Golang : Save map/struct to JSON or XML file
+9.7k Golang : Populate slice with sequential integers example