I am writing some code that will check the aspect ratio of an incoming/original image, and report if the image is x/y outside of a list of possible ratios. While I can determine the ratio with a little bit of math, I am wondering if aspect ratio of an image might be as easily available as the width or height of an image?
Thanks!
How do I determine the aspect ratio of an image?
- fmw42
- Posts: 25562
- Joined: 2007-07-02T17:14:51-07:00
- Authentication code: 1152
- Location: Sunnyvale, California, USA
Re: How do I determine the aspect ratio of an image?
convert image -format "%[fx:w/h]" info:
if on windows, see http://www.imagemagick.org/Usage/windows/ for issues of doubling the % and other Windows differences.
if on windows, see http://www.imagemagick.org/Usage/windows/ for issues of doubling the % and other Windows differences.
Re: How do I determine the aspect ratio of an image?
Thank you very much for this information and please bear with me as I need some more advice.fmw42 wrote:convert image -format "%[fx:w/h]" info:
if on windows, see http://www.imagemagick.org/Usage/windows/ for issues of doubling the % and other Windows differences.
When I run the above on images coming in, I get .75 for images coming in with a 3/4 aspect ration and 1.333... for images coming in with a 4x3 aspect ratio. My system accepts images that are supposed to be either 3x4 or 4x3, allowing for a little bit of fudging because it seems that photoshop seems to size a bit differently than ImageMagick would.
So I am trying to determine if an image is 3 or more pixels outside of the 3x4 or 4x3 aspect ratio before resizing to hard coded sizes, and if it is too much outside the pre-determined aspect ratio, we will kick it out.
My plan is to do the following against the incoming images:
Given the information provided by "%[fx:w/h]", such as .75, multiply that against the height if the image is taller than wide, resulting in the width if it is truly 3x4:
image_s3x4.jpg JPEG 510x680
(.75 x 680) / .75 x height
Given the information provided by "%[fx:w/h]", such as .75, multiply that against the width if the image is wider than tall, resulting in the height if it is truly 4x3:
image_s4x3.jpg JPEG 3072x2304
(.75 x 3072) / .75 x width
And then, I plan to take the value provided, width or height, and see if it is more than 2 or so pixels off, determining if the incoming image is not quite fitting into a 3x4 or 4x3 image ratio.
Pardon the long post, but I'm essentially trying to determine if incoming images are "close" to a 3x4 or 4x3 image ratio, and any advice regarding my technique would be greatly appreciated.