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
+5.3k Python : Convert(cast) string to bytes example
+17.7k How to enable MariaDB/MySQL logs ?
+13.5k Golang : Count number of runes in string
+16.9k Golang : How to generate QR codes?
+6.8k Get Facebook friends working in same company
+12.7k Golang : Pass database connection to function called from another package and HTTP Handler
+13k Golang : Calculate elapsed years or months since a date
+5.1k Linux : How to set root password in Linux Mint
+37.5k Golang : Converting a negative number to positive number
+6.3k Golang : Test input string for unicode example
+34.6k Golang : How to stream file to client(browser) or write to http.ResponseWriter?
+23k Golang : Calculate time different