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
+9.2k Golang : How to control fmt or log print format?
+6.9k How to let Facebook Login button redirect to a particular URL ?
+24.5k Golang : GORM read from database example
+5.2k Golang : Issue HTTP commands to server and port example
+8.2k Golang : Get final or effective URL with Request.URL example
+9.3k Golang : How to get garbage collection data?
+6.3k Golang : How to get capacity of a slice or array?
+29.7k Golang : Record voice(audio) from microphone to .WAV file
+10.7k Golang : Underscore string example
+23.9k Golang : Fix type interface{} has no field or no methods and type assertions example
+8.7k Golang : Combine slices but preserve order example
+11.5k Golang : Change date format to yyyy-mm-dd