Does anyone have ideas for a macro that would show you the dimensions of a selected image file in Finder? (Without you even having to open the file.)

Ideally, this wouldn't show you the pixel dimensions, but would show you the simplified dimensions. That is, instead of or in addition to showing you, say, "800px x 1,000px" for an image, it would show you "4x5".

I hope what I'm describing makes sense. It would be great to hear any ideas folks have. I'd really only need this to work for JGPs and PNGs, in case it makes things easier.

Here's one way to get an approximation of the aspect ratio. I say approximation because I cut it off at three digits, so if you see 1.333, it's really 1.3 repeating, or 4:3. There's probably some way to make it actually spit out "4:3," but I don't know what it isβI mainly wanted to show how to grab and work with the pixel dimensions.

But this uses the file command and a regexp to dig out the width and height dimensions in pixels of the image files selected in the Finder.

Then it calculates the aspect ratio as a single number (like 1.5).

Then I cheat with a switch action that looks for a few common photo aspect ratios to format the aspect ratio as a ration (like 3:2). Or just report the other format.

That's really funny, as I contemplated the same thing (table of standard ratios), then decided "Nah, there must be a slicker way to do it, hopefully someone else points out how" :).

New version (April 23): This saves the output for each file and display all of the results in one window. It also changes all the append to variable output to save to variable to overwrite old data.

That bit made my head hurt -- either I never learnt it or, more likely, I've completely forgotten! Either way, thanks for sending me down the rabbit hole

So...

Spotlight metadata will might (thanks, @mrpasini) be the fastest/most efficient way to get the image data we need -- and, for those files that haven't been indexed, there's our old friend sips. Putting those together with a KM version of @mrpasini's Euclidean algorithm and, if I've got it right:

Thanks for that. I actually started with sips but abandoned it for file only because I had another error I misattributed to sips. I was looking for something other than ImageMagick .

Is there a noticeable speed difference between Spotlight, sips and file? I guess I should try the two versions on my 2012 vintage MacBook Pro to see.

Yes, I have been rather too definitive there, haven't I? I hadn't realised how fast file is, either. I thought Spotlight would be the fastest/most efficient, given that this is indexed, pre-collected, metadata -- but I was forgetting that we are directly querying a specific file rather searching for files that match X.

Off to find a large image file on a slow network share to do some tests...

I merged all the macros together (in the order they were posted, but using the second version from @mrpasini), and added my timer to see how they did. Here are the results:

0.322: griffman
0.445: peternlewis
0.306: mrpasini
0.303: Nige_S
0.061: Unallocated time
βββββββββββββ
1.437: TOTAL TIME

So really, not a lot of time difference between the various methods. And here's how the combined output looks:

griffman
-----------
/Users/robg/Desktop/puzzlepix/IMG_3364.jpeg [5712x4284] β 1.333:1 aspect ratio
peternlewis
---------------
0.75
mrpasini
------------
/Users/robg/Desktop/puzzlepix/IMG_3364.jpeg's aspect ratio is 4:3 [5712x4284 pixels].
Nige_S
----------
The ratio is:
3:4

@Nige_S and @peternlewis, I think you have the height and the width reversed? The ratio for my demo pic is definitely 4:3.

G'ah -- I was relying on mdls returning info in the order asked, with just a single grep for numbers at ends of lines. Easily fixed with two tighter patterns instead.

Actually that didn't seem to add any time at allβthis run was actually a bit quicker. (With times this short, there's a fair amount of variability from run to run ... though @mrpasini's time improved, too, so his was quicker )

0.307: griffman
0.435: peternlewis
0.275: mrpasini
0.286: Nige_S
0.054: Unallocated time
βββββββββββββ
1.357: TOTAL TIME

Indeed it has! Thanks very much for timing the versions.

I do think it's worth applauding that Keyboard Maestro itself accommodates so many different ways of tackling this. So as long as you've got any kind of rope, it will provide a scaffold for you to hang yourself.

@mrpasini@griffman@peternlewis@Nige_S, you guys are all amazing! I never cease to be impressed by the ingenuity and enthusiasm of the people in this forum. I'm grateful.

Is there an approach that would show you the aspect ratio in "#:#" format as long as the image is within a Β±1% threshold of one of the common aspect ratios? (In addition, the exact ratio would always be shown.)

For example, let's say we're talking about an image that's roughly 4:3.

If the image is between 1.320:1.000 and 1.347:1.000, i.e. within the 1% error range, it would display "4:3" along with the exact aspect ratio, e.g. "4:3 (1.325:1.000)".

If the image is outside of this and the other common aspect ratio ranges, it would simply display the exact ratio. For example, an image that's 1.350:1.000 would simply display "1.350:1.000" since it falls just outside the 4:3 error range.

Does that make sense?

EDIT: A 1% error range might be too large, now that I think about it. Maybe 0.5% would be better? Or maybe there could be a way within the macro to easily adjust the size of the error range?

Just combine the two methods above -- test for the common ratios first, including your error margin, then do the full calculation. I've used "If" because then you can combine < and > in one calculation -- I think "Switch/Case" would require would more entries. So you'd prompt for Local_ff ("fiddle factor"), which here is hard-coded for +/-0.5%, and do something like:

...and do a similar "If" for each of your standard ratios. If you have a lot of standards you could make it a bit shorter by always doing a landscape ratio, flipping your inputs if height > width and then flipping your output at the end.

To add on to what @Nige_S recommends, the fractional expression represents the width of the image relative to its height. So a 3:2 image (w:h) is 1.5x wider than tall.

Here are some common still aspect ratios (obtained by dividing the width by the height):

When image files are generated directly from screenshots, the file will probably reflect the screen's exact resolution. But there's a problem with that. A monitor's advertised aspect ratio is not the same thing as its true aspect ratio. This is done for advertising purposes. For example, nobody wants to talk about a 64:27 aspect ratio when 21:9 is roughly the same ratio and easier to remember. Yet when you divide these two aspect ratios, they aren't QUITE the same number. Close, but not the same. The Euclidean algorithm is able to calculate only the true aspect ratio.

Here's an article from the internet which shows people talking about a 2560x1080 display as being a 21:9 aspect ratio when 2560/1080=2.370370370 and 21/9=2.333333333. If you took a screenshot from this display, the true aspect ratio of the resulting file would be 64:27 not the advertised value of 21:9.

So if the goal is to get the "true" aspect ratio, then using the Euclidean Algorithm is fine. But if the goal is to get the "advertised aspect ratio" (and I suspect that's what the original poster really wants) then there is more work to do. Taking a step up from the Euclidean Algorithm would be to use Continued Fractions, which should be able (with some finessing) to find the same ratios that the marketing departments for the monitor manufacturers came up with. If you used a Continued Fraction algorithm for 2560:1080 you would come up with BOTH answers, 21:9 and 64:27.

Here's how big the problem really is:

Many 5:4 monitors are falsely advertised as 5:4 but have true ratios of 32:27 and 11:9.
Many 3:2 monitors are falsely advertised as 3:2 but have true ratios of 22:15.
Many 16:10 monitors are falsely advertised as 16:10 but have true ratios of 25:16.
Many 15:9 monitors are falsely advertised as 15:9 but have true ratios of 128:75.
Many 16:9 monitors are falsely advertised as 16:9 but have true ratios of 133:75, 71:40, 9:5 or 64:35.
Many 17:9 monitors are falsely advertised as 17:9 but have true ratios of 256:135.
Many 18:9 monitors are falsely advertised as 18:9 but have true ratios of 18.5:9, 19:9 or 19.5:9.
Many 21:9 monitors are falsely advertised as 21:9 but have true ratios of 20:9, 64:27, 21.5:9, 12:5.