Skip to content

Instantly share code, notes, and snippets.

@sergiotapia
Last active November 16, 2024 05:40
Show Gist options
  • Save sergiotapia/7882944 to your computer and use it in GitHub Desktop.
Save sergiotapia/7882944 to your computer and use it in GitHub Desktop.
Golang - Getting the dimensions of an image. jpg, jpeg, png
package main
import (
"fmt"
"image"
"os"
_ "image/jpeg"
_ "image/png"
)
func main() {
width, height := getImageDimension("rainy.jpg")
fmt.Println("Width:", width, "Height:", height)
}
func getImageDimension(imagePath string) (int, int) {
file, err := os.Open(imagePath)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
}
image, _, err := image.DecodeConfig(file)
if err != nil {
fmt.Fprintf(os.Stderr, "%s: %v\n", imagePath, err)
}
return image.Width, image.Height
}
@CypherpunkSamurai
Copy link

this also applies:

image..Bound().Dx - width
image.
.Bound().Dy - height

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment