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
+31.5k Golang : Example for ECDSA(Elliptic Curve Digital Signature Algorithm) package functions
+22.2k Golang : Print leading(padding) zero or spaces in fmt.Printf?
+7.5k Golang : Handling Yes No Quit query input
+7.4k Linux : How to fix Brother HL-1110 printing blank page problem
+9.8k Golang : Get current, epoch time and display by year, month and day
+9k Golang : How to use Gorilla webtoolkit context package properly
+46.5k Golang : Marshal and unmarshal json.RawMessage struct example
+5.9k AWS S3 : Prevent Hotlinking policy
+14k Golang : Google Drive API upload and rename example
+6.3k Apt-get to install and uninstall Golang
+8.3k Golang : Count leading or ending zeros(any item of interest) example
+7.4k Golang : Accessing dataframe-go element by row, column and name example