(
image: Image.Image,
shape: tuple[int, int],
)
| 123 | |
| 124 | |
| 125 | def resize_to_cover( |
| 126 | image: Image.Image, |
| 127 | shape: tuple[int, int], |
| 128 | ) -> tuple[ |
| 129 | Image.Image, # the image itself |
| 130 | tuple[int, int], # image shape after scaling, before cropping |
| 131 | ]: |
| 132 | w_old, h_old = image.size |
| 133 | h_new, w_new = shape |
| 134 | |
| 135 | # Figure out the scale factor needed to cover the desired shape with a uniformly |
| 136 | # scaled version of the input image. Then, resize the input image. |
| 137 | scale_factor = max(h_new / h_old, w_new / w_old) |
| 138 | h_scaled = round(h_old * scale_factor) |
| 139 | w_scaled = round(w_old * scale_factor) |
| 140 | image_scaled = image.resize((w_scaled, h_scaled), Image.LANCZOS) |
| 141 | |
| 142 | # Center-crop the image. |
| 143 | x = (w_scaled - w_new) // 2 |
| 144 | y = (h_scaled - h_new) // 2 |
| 145 | image_cropped = image_scaled.crop((x, y, x + w_new, y + h_new)) |
| 146 | return image_cropped, (h_scaled, w_scaled) |
| 147 | |
| 148 | |
| 149 | def resize_to_cover_with_intrinsics( |
no outgoing calls
no test coverage detected