| 73 | |
| 74 | |
| 75 | def _scale_image( |
| 76 | image: np.ndarray, |
| 77 | aspect_ratio: float | None, |
| 78 | height: int | None, |
| 79 | width: int | None, |
| 80 | scale: float | None, |
| 81 | interpolation: Interpolation, |
| 82 | ) -> np.ndarray: |
| 83 | # TODO: Combine this resize with the ones below. |
| 84 | if aspect_ratio is not None: |
| 85 | image = cv2.resize( |
| 86 | image, (0, 0), fx=aspect_ratio, fy=1.0, interpolation=interpolation.value |
| 87 | ) |
| 88 | image_height = image.shape[0] |
| 89 | image_width = image.shape[1] |
| 90 | |
| 91 | # Figure out what kind of resizing needs to be done |
| 92 | if height or width: |
| 93 | if height and not width: |
| 94 | factor = height / float(image_height) |
| 95 | width = int(factor * image_width) |
| 96 | elif width and not height: |
| 97 | factor = width / float(image_width) |
| 98 | height = int(factor * image_height) |
| 99 | assert height is not None |
| 100 | assert width is not None |
| 101 | assert height > 0 and width > 0 |
| 102 | image = cv2.resize(image, (width, height), interpolation=interpolation.value) |
| 103 | elif scale: |
| 104 | image = cv2.resize(image, (0, 0), fx=scale, fy=scale, interpolation=interpolation.value) |
| 105 | return image |
| 106 | |
| 107 | |
| 108 | class _ImageExtractor: |