Selects the best resolution from a list of possible resolutions based on the original size. Args: original_size (tuple): The original size of the image in the format (width, height). possible_resolutions (list): A list of possible resolutions in the format [(width1, height1
(original_size, possible_resolutions)
| 10 | |
| 11 | |
| 12 | def select_best_resolution(original_size, possible_resolutions): |
| 13 | """ |
| 14 | Selects the best resolution from a list of possible resolutions based on the original size. |
| 15 | |
| 16 | Args: |
| 17 | original_size (tuple): The original size of the image in the format (width, height). |
| 18 | possible_resolutions (list): A list of possible resolutions in the format [(width1, height1), (width2, height2), ...]. |
| 19 | |
| 20 | Returns: |
| 21 | tuple: The best fit resolution in the format (width, height). |
| 22 | """ |
| 23 | original_width, original_height = original_size |
| 24 | best_fit = None |
| 25 | max_effective_resolution = 0 |
| 26 | min_wasted_resolution = float('inf') |
| 27 | |
| 28 | for width, height in possible_resolutions: |
| 29 | scale = min(width / original_width, height / original_height) |
| 30 | downscaled_width, downscaled_height = int(original_width * scale), int(original_height * scale) |
| 31 | effective_resolution = min(downscaled_width * downscaled_height, original_width * original_height) |
| 32 | wasted_resolution = (width * height) - effective_resolution |
| 33 | |
| 34 | if effective_resolution > max_effective_resolution or (effective_resolution == max_effective_resolution and wasted_resolution < min_wasted_resolution): |
| 35 | max_effective_resolution = effective_resolution |
| 36 | min_wasted_resolution = wasted_resolution |
| 37 | best_fit = (width, height) |
| 38 | |
| 39 | return best_fit |
| 40 | |
| 41 | |
| 42 | def resize_and_pad_image(image, target_resolution): |
no outgoing calls
no test coverage detected