Resize the image to fit within the specified width and height, maintaining the aspect ratio, and then center the image within the dimensions, cropping the excess. Args: image: The image to resize. width: The width to resize the image to. height:
(
self,
image: PIL.Image.Image,
width: int,
height: int,
)
| 303 | return res |
| 304 | |
| 305 | def _resize_and_crop( |
| 306 | self, |
| 307 | image: PIL.Image.Image, |
| 308 | width: int, |
| 309 | height: int, |
| 310 | ) -> PIL.Image.Image: |
| 311 | """ |
| 312 | Resize the image to fit within the specified width and height, maintaining the aspect ratio, and then center the image within the dimensions, cropping the excess. |
| 313 | |
| 314 | Args: |
| 315 | image: The image to resize. |
| 316 | width: The width to resize the image to. |
| 317 | height: The height to resize the image to. |
| 318 | """ |
| 319 | ratio = width / height |
| 320 | src_ratio = image.width / image.height |
| 321 | |
| 322 | src_w = width if ratio > src_ratio else image.width * height // image.height |
| 323 | src_h = height if ratio <= src_ratio else image.height * width // image.width |
| 324 | |
| 325 | resized = image.resize((src_w, src_h), resample=PIL_INTERPOLATION["lanczos"]) |
| 326 | res = Image.new("RGB", (width, height)) |
| 327 | res.paste(resized, box=(width // 2 - src_w // 2, height // 2 - src_h // 2)) |
| 328 | return res |
| 329 | |
| 330 | def resize( |
| 331 | self, |