I want to resize a bunch of images down really tiny so that I can perform some image analysis on them. I want them all to contain the same number of pixels for my vector comparisons. I chose "120" because it's highly composite. I could resize every image to 12x10, but then there may be more stretching than necessary for images that do not have a 1.2 aspect ratio.
How can I choose a new width and height that most closely matches the original aspect ratio?
For reference, the divisors of 120 are: {1, 2, 3, 4, 5, 6, 8, 10, 12, 15, 20, 24, 30, 40, 60, 120}, so valid sizes would be 12x10, 10x12, 8x15, 15x8, 6x20, 20x6 and so forth.
Edit: 144 might have been a better choice, as it allows for square images and the popular 16:9 ratio.
The way I'd implement something like this is to store the aspect ratios (1:144, 2:72, 3:48, 4:36, etc.) into a sorted array. Then for each incoming image, calculate its aspect ratio, then find the nearest desired ratio using binary search.
Even better, store the log of the aspect ratios, and do the binary search using the log of the image's aspect ratio.