Golang image.Image type example

package image

Image is a finite rectangular grid of color.Color values taken from a color model.

Golang image.Image type usage example

 package main

 import (
 "fmt"
 "image"
 "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()
 cmodel := img.ColorModel()
 at := img.At(100,100)


 fmt.Println(bounds)
 fmt.Println(cmodel)
 fmt.Println(at)
 }

Sample output(depending on input image) :

(0,0)-(600,849)

&{0x7ec10}

{255 255 255 255}

Reference :

http://golang.org/pkg/image/#Image

Advertisement