Golang image.DecodeConfig function example
package image
func DecodeConfig(r io.Reader) (Config, string, error)
DecodeConfig decodes the color model and dimensions of an image that has been encoded in a registered format. The string returned is the format name used during format registration. Format registration is typically done by an init function in the codec-specific package.
Golang image.DecodeConfig function usage example
package main
import (
"fmt"
"image"
"image/png"
"io/ioutil"
"os"
"bytes"
)
func init() {
image.RegisterFormat("png", "png", png.Decode, png.DecodeConfig)
}
func main() {
imgBuffer, err := ioutil.ReadFile("./img.png")
if err != nil {
fmt.Println("img.png file not found!")
os.Exit(1)
}
reader := bytes.NewReader(imgBuffer)
iconf, formatname, err := image.DecodeConfig(reader)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
//test
fmt.Println("Format : ", formatname)
fmt.Println("Width : ", iconf.Width)
fmt.Println("Height : ", iconf.Height)
fmt.Println("ColorModel : ", iconf.ColorModel)
}
Sample output (depending on image file ) :
Format : png
Width : 600
Height : 849
ColorModel : &{0x821e0}
Reference :
Advertisement
Something interesting
Tutorials
+16.4k Golang : Test floating point numbers not-a-number and infinite example
+7.6k Javascript : Push notifications to browser with Push.js
+17.3k Golang : How to tell if a file is compressed either gzip or zip ?
+5.7k Get website traffic ranking with Similar Web or Alexa
+22.7k Golang : Round float to precision example
+9.7k Golang : interface - when and where to use examples
+15k Golang : package is not in GOROOT during compilation
+14.6k Golang : Convert(cast) int to float example
+4.9k Unix/Linux : secure copying between servers with SCP command examples
+13.4k Golang : Read from buffered reader until specific number of bytes
+6.8k Golang : Calculate pivot points for a cross