(image, target_area, keep_aspect_ratio=True, divisor=64, padding_color=(0, 0, 0))
| 103 | |
| 104 | |
| 105 | def resize_by_area(image, target_area, keep_aspect_ratio=True, divisor=64, padding_color=(0, 0, 0)): |
| 106 | h, w = image.shape[:2] |
| 107 | try: |
| 108 | new_w, new_h = calculate_new_size(w, h, target_area, divisor) |
| 109 | except: # noqa |
| 110 | aspect_ratio = w / h |
| 111 | |
| 112 | if keep_aspect_ratio: |
| 113 | new_h = math.sqrt(target_area / aspect_ratio) |
| 114 | new_w = target_area / new_h |
| 115 | else: |
| 116 | new_w = new_h = math.sqrt(target_area) |
| 117 | |
| 118 | new_w, new_h = int((new_w // divisor) * divisor), int((new_h // divisor) * divisor) |
| 119 | |
| 120 | interpolation = cv2.INTER_AREA if (new_w * new_h < w * h) else cv2.INTER_LINEAR |
| 121 | |
| 122 | resized_image = padding_resize(image, height=new_h, width=new_w, padding_color=padding_color, interpolation=interpolation) |
| 123 | return resized_image |
| 124 | |
| 125 | |
| 126 | def padding_resize(img_ori, height=512, width=512, padding_color=(0, 0, 0), interpolation=cv2.INTER_LINEAR): |
no test coverage detected