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
+12k Golang : Find and draw contours with OpenCV example
+8.8k Golang : Executing and evaluating nested loop in html template
+9.4k Facebook : Getting the friends list with PHP return JSON format
+13.4k Golang : Read from buffered reader until specific number of bytes
+17.6k Golang : Parse date string and convert to dd-mm-yyyy format
+40.1k Golang : UDP client server read write example
+11.1k Golang : Read until certain character to break for loop
+23.5k Golang : Read a file into an array or slice example
+5.4k Golang : Return multiple values from function
+7.3k Golang : How to convert strange string to JSON with json.MarshalIndent
+8.8k Golang : Accept any number of function arguments with three dots(...)
+9k Golang : Inject/embed Javascript before sending out to browser example