Photo Corners headlinesarchivemikepasini.com


A   S C R A P B O O K   O F   S O L U T I O N S   F O R   T H E   P H O T O G R A P H E R

Enhancing the enjoyment of taking pictures with news that matters, features that entertain and images that delight. Published frequently.

Calculating an Aspect Ratio Share This on LinkedIn   Tweet This   Forward This

23 April 2024

We should have known better, really. But it seemed like such a simple request. A poster on the Keyboard Maestro forum asked, "Does anyone have ideas for a macro that would show you the dimensions of a selected image file in Finder?"

Aspect Ratio. This macro derives the aspect ratio from an images's width and height. Which is harder than it sounds..

They didn't mean the height and width. They wanted to know the aspect ratio.

On macOS, getting the height and width of an image selected in the Finder isn't much work. There are several ways to do it.

And to its credit, Keyboard Maestro is happy to accommodate whichever way you prefer. So it was that griffman and Nige_S and yours truly came up with three different approaches.

But what stymied all of us was turning the simple height/width formula for aspect ratio from a single number (like 1.33) into a ratio (4:3).

That's because it isn't a simple calculation but several.

We stumbled around googling for an answer without any success. Google can be quite hard headed about returning irrelevant results and ignoring what you're really asking for.

So we turned to Wavelength, which lets us ask AI for answers. That's what it calls it. AI. So we wrote to AI, asking, "How do I derive an aspect ratio with the format ##:## from the width and height of an image?"

AI answered that we had to find the Greatest Common Divisor of the two numbers. Which we found not very helpful. So we persisted and AI finally revealed:

The formula for finding the greatest common divisor (GCD) of two numbers is typically calculated using the Euclidean algorithm. This algorithm involves repeatedly dividing the larger number by the smaller number and replacing the larger number with the remainder until the remainder is 0. The last non-zero remainder is the GCD.

We tried doing that manually to see how it works and confirmed that it does. The trick became how to convert our manual calculations into code.

We realized immediately that we didn't want to work this series of calculations out with a series of Keyboard Maestro actions. We thought it would be more efficient, faster even, to do it in something like Perl where we could write a recursive function and get it all done in one action.

So we looked for a Perl function that implements the Euclidean algorithm of 2,300 years ago. And the Perl Monks had one (or four). We tried a few of them before settling on one of the more concise ones.

Our macro will process as many images as you've selected in the Finder, reporting their dimensions and aspect ratio in one window.

It uses the find command to dig out the pixel dimensions of the image rather than the mdls command griffman used or the combination of mdls and sips the Nige_S preferred.

Interestingly, griffman did some timing tests of the three approaches. If our approach was quicker, we suspect it was because we used fewer actions, relying on the Perl code in one action instead.

But they were all quick. And it was a fun puzzle to work out.


BackBack to Photo Corners