I have a list of image resolution dictionaries:
[
{'ratio': 1.7777777777777777, 'pixels': 230400, 'key': 'hor_640x360'},
{'ratio': 2.1006711409395975, 'pixels': 46637, 'key': 'hor_313x149'},
{'ratio': 2.1006711409395975, 'pixels': 746192, 'key': 'hor_1252x596'}
]
I need to select a dictionary whose relation is closer to 1.77672955975(that is, the result float(565) / float(318)), and if the ratio is equal, then with the largest number of pixels. In other words, I need to select the largest image from the list, which after resizing and cropping to 565x318 will require less cropping.
I thought about sorting the list 2 times, the first time based on pixels, and the second based on a smaller result abs(ratio - (float(565) / float(318))). Thus, the one with the largest number of pixels and the closest match will remain on top. But how to do sorting?
source
share