Find the nearest fitting image

34 Views Asked by At

Say that I need to resize an image into 104cm x 232cm (width x height). I have various different folders to pick an image from to resize, while each folder is of different sizes.

"80x200", "90x200", "90x205", "90x210", "90x230", "100x200", "100x210", "100x230","110x200", "110x210", "110x230", "120x200", "120x210", "120x230", "130x230"

Now the challenge here is to find a folder that comes close to the required image size. In the case of 104x232, the answer is 100x230, because when resized it will look proper.

Below equation should give me the ratio difference for each folder, with which I should be able to get the folder that's close to the required size.

ratio_distance = abs((folder_x_width / folder_x_height) - (new_width / new_height))

For example, this would give me the following,

  1. ratio_90x200 = abs((90 / 200) - ( 104 / 232)) = 0.0017241379
  2. ratio_100x230 = abs((100 / 230) - ( 104 / 232)) = 0.0134932534

According this, image found in the 90x200 folder is more suitable than the folder 100x230, while the expected result is 100x230.

How could I improve the above to get the desired output?

1

There are 1 best solutions below

1
On BEST ANSWER

The $90 \times 200$ image is closer in aspect ratio to $104 \times 232$ than $100 \times 230$ is. If you want the least distortion of aspect ratio so circles come out closest to circles $90 \times 200$ is a better choice. Your approach is choosing the closest aspect ratio. If you want a different answer you need to define what your criterion is.