Golang: Get Image Size
/* File name: get_image_size.go Created date: 2024-11-23 Modified date: 2024-11-24 Home page: http://xahlee.info/golang/golang_get_image_width_height.html take a image file path as arg. return image width and height as a string, e.g. "1200 800" run it like this go run .\get_image_size.go "c:/Users/xah/Downloads/2024-10-18_image.jpg" todo, right now only work on jpg. make it work on png and other. */ package main import ( "fmt" "image/jpeg" "os" ) func main() { // Open the file file, err := os.Open(os.Args[1]) if err != nil { fmt.Println("Error opening file:", err) return } defer file.Close() // Decode the JPEG image img, err := jpeg.Decode(file) if err != nil { fmt.Println("Error decoding JPEG:", err) return } // Get the width and height of the image bounds := img.Bounds() width, height := bounds.Max.X, bounds.Max.Y fmt.Printf("%d %d", width, height) }