Golang image.Gray.Set and SetGray functions example

package image

 func (p *Gray) Set(x, y int, c color.Color)
  

Golang image.Gray.Set and SetGray functions usage example

 package main

 import (
 "fmt"
 "image"
 "image/color" // for color.Alpha{a}
 "image/png"
 "os"
 )

 func init() {
 // without this register .. At(), Bounds() functions will
 // caused memory pointer error!!
 image.RegisterFormat("png", "png", png.Decode, png.DecodeConfig)
 }

 func main() {
 imgfile, err := os.Open("./img.png")

 if err != nil {
 fmt.Println("img.png file not found!")
 os.Exit(1)
 }

 defer imgfile.Close()

 img, _, err := image.Decode(imgfile)

 bounds := img.Bounds()

 canvas := image.NewGray(bounds) // for Gray

 canvas.Set(100, 100, image.Transparent)

 // http://golang.org/pkg/image/color/#Gray
 // Gray represents an 8-bit grayscale color.

 x := 512
 y := 512
 a := uint8(255)

 canvas.SetGray(x, y, color.Gray{a})

 }

References :

http://golang.org/pkg/image/#Gray.Set

http://golang.org/pkg/image/#Gray.SetGray

Advertisement