Skip to content

Instantly share code, notes, and snippets.

@sergiotapia
Last active February 13, 2024 17:40
Show Gist options
  • Star 44 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • 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
}
@Deleplace
Copy link

Thanks! I had never properly noticed the existence of func image.DecodeConfig before, which is great for me because I don't want to decode the whole image just to know its dimensions.

@mkyung
Copy link

mkyung commented Jun 29, 2019

If you already have loaded an image with image.Decode(), you can also

b := img.Bounds()
imgWidth := b.Max.X
imgHeight := b.Max.Y

Great advice! Thanks

@aprln
Copy link

aprln commented Jul 28, 2020

If you already have loaded an image with image.Decode(), you can also

b := img.Bounds()
imgWidth := b.Max.X
imgHeight := b.Max.Y

I needed something similar and this pointed me to a good direction. I used it like this though

b := img.Bounds()
width := b.Dx()
height := b.Dy()

img.Bounds() returns a Rectangle. Below is extract from the image package source code.


// A Rectangle contains the points with Min.X <= X < Max.X, Min.Y <= Y < Max.Y.
// It is well-formed if Min.X <= Max.X and likewise for Y. Points are always
// well-formed. A rectangle's methods always return well-formed outputs for
// well-formed inputs.
//
// A Rectangle is also an Image whose bounds are the rectangle itself. At
// returns color.Opaque for points in the rectangle and color.Transparent
// otherwise.
type Rectangle struct {
	Min, Max Point
}

// String returns a string representation of r like "(3,4)-(6,5)".
func (r Rectangle) String() string {
	return r.Min.String() + "-" + r.Max.String()
}

// Dx returns r's width.
func (r Rectangle) Dx() int {
	return r.Max.X - r.Min.X
}

// Dy returns r's height.
func (r Rectangle) Dy() int {
	return r.Max.Y - r.Min.Y
}

@cnankit
Copy link

cnankit commented Feb 15, 2021

If you already have loaded an image with image.Decode(), you can also

b := img.Bounds()
imgWidth := b.Max.X
imgHeight := b.Max.Y

I needed something similar and this pointed me to a good direction. I used it like this though

b := img.Bounds()
width := b.Dx()
height := b.Dy()

img.Bounds() returns a Rectangle. Below is extract from the image package source code.


// A Rectangle contains the points with Min.X <= X < Max.X, Min.Y <= Y < Max.Y.
// It is well-formed if Min.X <= Max.X and likewise for Y. Points are always
// well-formed. A rectangle's methods always return well-formed outputs for
// well-formed inputs.
//
// A Rectangle is also an Image whose bounds are the rectangle itself. At
// returns color.Opaque for points in the rectangle and color.Transparent
// otherwise.
type Rectangle struct {
	Min, Max Point
}

// String returns a string representation of r like "(3,4)-(6,5)".
func (r Rectangle) String() string {
	return r.Min.String() + "-" + r.Max.String()
}

// Dx returns r's width.
func (r Rectangle) Dx() int {
	return r.Max.X - r.Min.X
}

// Dy returns r's height.
func (r Rectangle) Dy() int {
	return r.Max.Y - r.Min.Y
}

Thanks! found this very helpful for my case. :)

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