Last active
November 16, 2024 05:40
-
-
Save sergiotapia/7882944 to your computer and use it in GitHub Desktop.
Golang - Getting the dimensions of an image. jpg, jpeg, png
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
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
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
}
If you already have loaded an image with image.Decode(), you can also
b := img.Bounds() imgWidth := b.Max.X imgHeight := b.Max.YI 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 aRectangle
. Below is extract from theimage
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. :)
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
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.