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
+6k Golang : How to verify input is rune?
+55.3k Golang : Unmarshal JSON from http response
+10.2k Golang : Find and replace data in all files recursively
+8.4k Golang : Generate Datamatrix barcode
+19.4k Golang : How to count the number of repeated characters in a string?
+25.5k Golang : Generate MD5 checksum of a file
+9.6k Golang : Copy map(hash table) example
+7.4k Golang : Hue, Saturation and Value(HSV) with OpenCV example
+6.6k Golang : How to validate ISBN?
+10.5k Golang : Create matrix with Gonum Matrix package example
+15.8k Golang : Get digits from integer before and after given position example
+25k Golang : Create PDF file from HTML file