Golang image.Config type example

package image

  type Config struct {
 ColorModel color.Model
 Width, Height int
 }
  

Config holds an image's color model and dimensions.

Golang image.Config 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()

 width := bounds.Max.X
 height := bounds.Max.Y

 fmt.Println("Width : ", width)
 fmt.Println("Height : ", height)

 colormodel := img.ColorModel()

 ic := image.Config{colormodel, width, height}

 //test
 fmt.Println(ic.Width) // Width starts with capital letter

 }

References :

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

Advertisement