How to get Dimensions of an image type jpg jpeg png or gif ?
The return type of DecodeConfig config function is struct image which have 2 int variables Width and Height through which you get the dimensions of any jpg jpeg gif or png image in Go.
Example
package main
import (
"fmt"
"image"
"os"
_ "image/jpeg"
_ "image/png"
_ "image/gif"
)
func main() {
imagePath := "jellyfish.jpg"
file, err := os.Open(imagePath)
defer file.Close();
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
}
image, _, err := image.DecodeConfig(file) // Image Struct
if err != nil {
fmt.Fprintf(os.Stderr, "%s: %v\n", imagePath, err)
}
fmt.Println("Width:", image.Width, "Height:", image.Height)
}
Output
Width: 758 Height: 569
Most Helpful This Week
Normal function parameter with variadic function parameter
Converting Int data type to Float in Go
Passing multiple string arguments to a variadic function
How to read current directory using Readdir?
Golang Convert String into Snake Case
Example of Fscan, Fscanf, and Fscanln from FMT Package
How to Decode or Unmarshal bi-dimensional array of integers?
How to update content of a text file?
Get Set and Clear Session in Golang
Find out how many logical processors used by current process