Skip to main content
Participating Frequently
June 12, 2020
Answered

Get Aspect Ratio from Cropped Dimensions [LUA, Plugin, SDK]

  • June 12, 2020
  • 3 replies
  • 1157 views

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

This topic has been closed for replies.
Correct answer johnrellis

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.]

3 replies

johnrellis
Legend
June 16, 2020

Good. Which version of "match" are you using?  

 

[Use the blue reply button under the first post to ensure replies sort properly.]

Participating Frequently
June 16, 2020

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
johnrellis
johnrellisCorrect answer
Legend
June 12, 2020

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.]

Rob_Cullen
Community Expert
Community Expert
June 13, 2020

"fuzzy" is the word.

I knew you, John, would have an answer. 🙂

Regards. My System: Windows-11, Lightroom-Classic 15.3, Photoshop 27.5, ACR 18.3, Lightroom 9.3, Lr-iOS 10.4.0, Bridge 16.0.3 .
Rob_Cullen
Community Expert
Community Expert
June 12, 2020

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!

Regards. My System: Windows-11, Lightroom-Classic 15.3, Photoshop 27.5, ACR 18.3, Lightroom 9.3, Lr-iOS 10.4.0, Bridge 16.0.3 .
Participating Frequently
June 13, 2020

Thanks WobertC, but I am glad John came with an easier option - maths was never a strong subject for me 🙂