| 124 | |
| 125 | |
| 126 | def center_crop_arr(pil_image, image_size): |
| 127 | # We are not on a new enough PIL to support the `reducing_gap` |
| 128 | # argument, which uses BOX downsampling at powers of two first. |
| 129 | # Thus, we do it by hand to improve downsample quality. |
| 130 | while min(*pil_image.size) >= 2 * image_size: |
| 131 | pil_image = pil_image.resize( |
| 132 | tuple(x // 2 for x in pil_image.size), resample=Image.BOX |
| 133 | ) |
| 134 | |
| 135 | scale = image_size / min(*pil_image.size) |
| 136 | pil_image = pil_image.resize( |
| 137 | tuple(round(x * scale) for x in pil_image.size), resample=Image.BICUBIC |
| 138 | ) |
| 139 | |
| 140 | arr = np.array(pil_image) |
| 141 | crop_y = (arr.shape[0] - image_size) // 2 |
| 142 | crop_x = (arr.shape[1] - image_size) // 2 |
| 143 | return arr[crop_y : crop_y + image_size, crop_x : crop_x + image_size] |
| 144 | |
| 145 | |
| 146 | def random_crop_arr(pil_image, image_size, min_crop_frac=0.8, max_crop_frac=1.0): |