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
+24.1k Golang : Upload to S3 with official aws-sdk-go package
+5.7k Unix/Linux/MacOSx : Get local IP address
+10k Golang : Get escape characters \u form from unicode characters
+9.1k Golang : Intercept and compare HTTP response code example
+6.9k Mac/Linux/Windows : Get CPU information from command line
+8.7k Golang : How to join strings?
+12.8k Golang : http.Get example
+5.8k Unix/Linux : Get reboot history or check when was the last reboot date
+26.9k Golang : Force your program to run with root permissions
+6.9k Golang : Pat multiplexer routing example
+14.4k Android Studio : Use image as AlertDialog title with custom layout example
+6.5k Golang : Spell checking with ispell example