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
+14.9k Golang : Basic authentication with .htpasswd file
+23.1k Golang : simulate tail -f or read last line from log file example
+9.9k Golang : Sort and reverse sort a slice of integers
+15.6k Golang : Force download file example
+9.8k Golang : Get current, epoch time and display by year, month and day
+12.2k Golang : calculate elapsed run time
+10.6k Golang : ISO8601 Duration Parser example
+31.7k Golang : How to convert(cast) string to IP address?
+6.2k Golang : Get Hokkien(福建话)/Min-nan(閩南語) Pronounciations
+5.2k Golang : PGX CopyFrom to insert rows into Postgres database
+14k Golang : concatenate(combine) strings
+9.5k Golang : Get all countries currencies code in JSON format