r""" 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 (`PIL.Image.Image`): The image to resize and crop. widt
(
self,
image: PIL.Image.Image,
width: int,
height: int,
)
| 427 | return res |
| 428 | |
| 429 | def _resize_and_crop( |
| 430 | self, |
| 431 | image: PIL.Image.Image, |
| 432 | width: int, |
| 433 | height: int, |
| 434 | ) -> PIL.Image.Image: |
| 435 | r""" |
| 436 | Resize the image to fit within the specified width and height, maintaining the aspect ratio, and then center |
| 437 | the image within the dimensions, cropping the excess. |
| 438 | |
| 439 | Args: |
| 440 | image (`PIL.Image.Image`): |
| 441 | The image to resize and crop. |
| 442 | width (`int`): |
| 443 | The width to resize the image to. |
| 444 | height (`int`): |
| 445 | The height to resize the image to. |
| 446 | |
| 447 | Returns: |
| 448 | `PIL.Image.Image`: |
| 449 | The resized and cropped image. |
| 450 | """ |
| 451 | ratio = width / height |
| 452 | src_ratio = image.width / image.height |
| 453 | |
| 454 | src_w = width if ratio > src_ratio else image.width * height // image.height |
| 455 | src_h = height if ratio <= src_ratio else image.height * width // image.width |
| 456 | |
| 457 | resized = image.resize((src_w, src_h), resample=PIL_INTERPOLATION[self.config.resample]) |
| 458 | res = Image.new("RGB", (width, height)) |
| 459 | res.paste(resized, box=(width // 2 - src_w // 2, height // 2 - src_h // 2)) |
| 460 | return res |
| 461 | |
| 462 | def resize( |
| 463 | self, |