Copy link to clipboard
Copied
I think I have been looking at this too long - as it should be fairly simple!
In my plug-in, I need to be able to categorise images by their aspect ratio (e.g., 1x1, 3x2, 4x3, 16x9, etc). Can anyone tell me the formula I can use (or an LUA function) that would return a "photo-friendly" aspect ratio from the photo's cropped dimensions?
Many thanks
Phil
WobertC's method won't quite work. Consider a 5472 x 3648 photo (a 3 x 2 ratio). When I crop that to 2338 wide, maintaining the original ratio, LR truncates the desired 1558 2/3 pixel height to 1558 (it should have rounded to 1559 rather than truncating, but LR gets much of the arithmetic of cropping a little wrong). Neither 2338 x 1558 nor 2338 x 1559 simplify to 3 x 2.
So instead, the code should search through the list of common photo ratios, looking for one that matches the cropped dimen
...Copy link to clipboard
Copied
Being curious, following thread for possible answers. Working from pixel dimensions to an aspect ratio is mathematics.
You are looking for math that finds the 'Ratio in lowest common denominator'
https://www.calculatorsoup.com/calculators/math/ratio-simplifier.php
[LUA, Plugin, SDK]- Way above my "pay grade"- sorry!
Copy link to clipboard
Copied
Thanks WobertC, but I am glad John came with an easier option - maths was never a strong subject for me 🙂
Copy link to clipboard
Copied
WobertC's method won't quite work. Consider a 5472 x 3648 photo (a 3 x 2 ratio). When I crop that to 2338 wide, maintaining the original ratio, LR truncates the desired 1558 2/3 pixel height to 1558 (it should have rounded to 1559 rather than truncating, but LR gets much of the arithmetic of cropping a little wrong). Neither 2338 x 1558 nor 2338 x 1559 simplify to 3 x 2.
So instead, the code should search through the list of common photo ratios, looking for one that matches the cropped dimensions, possibly off by one pixel:
local ratios = {{1, 1}, {3,2}, {4, 3}, {5, 4}, {16, 9}}
local function aspectRatio (w, h)
if w < h then w, h = h, w end
for _, ratio in ipairs (ratios) do
local rw, rh = ratio [1], ratio [2]
local delta = math.abs (rw / rh * h - w)
if delta <= 1 then return rw, rh end
end
return nil, nil
end
But note that there are many cameras that produce images claiming a 3 x 2 aspect ratio but that are in fact slightly different:
Nikon D90, 4288 x 2848 ~ 1.5056
Nikon D3200, 6016 x 4000 = 1.504
Nikon D750, 6016 x 4016 ~ 1.498
Canon EOS Rebel T1i, 5477 x 3651 ~ 1.50014
(These are just some of the cameras from my catalog that I looked at.) All but the last differ by more than one pixel in width from the 3 x 2 aspect ratio. So if you want to label these as "3 x 2", you'll need to use a "fuzzier" test:
local delta = math.abs (rw / rh - w / h)
if delta <= 0.006 then return rw, rh end
I don't have an exhaustive list of camera sensors, so I don't know if the maximum delta of 0.06 is the best value.
Of course, this "fuzzy" matching will include crops that also don't quite match the common ratios exactly (or off by one pixel).
[Use the blue reply button under the first post to ensure replies sort properly.]
Copy link to clipboard
Copied
"fuzzy" is the word.
I knew you, John, would have an answer. 🙂
Copy link to clipboard
Copied
Thanks John - I think that gives me a very good start. I will test it against my images and see if it can correctly pick-up the aspect ratios.
Phil
Copy link to clipboard
Copied
Works 🙂 Thanks for the function John
Copy link to clipboard
Copied
Good. Which version of "match" are you using?
[Use the blue reply button under the first post to ensure replies sort properly.]
Copy link to clipboard
Copied
Hi John
This version....
local ratios = {{1, 1}, {3,2}, {4, 3}, {5, 4}, {16, 9}}
local function aspectRatio (w, h)
if w < h then w, h = h, w end
for _, ratio in ipairs (ratios) do
local rw, rh = ratio [1], ratio [2]
local delta = math.abs (rw / rh * h - w)
if delta <= 1 then return rw, rh end
end
return nil, nil
end