Resize image. Args: image (`PIL.Image.Image`, `np.ndarray` or `torch.Tensor`): The image input, can be a PIL image, numpy array or pytorch tensor. height (`int`): The height to resize to. width (`int`):
(
self,
image: PIL.Image.Image | np.ndarray | torch.Tensor,
height: int,
width: int,
resize_mode: str = "default", # "default", "fill", "crop"
)
| 460 | return res |
| 461 | |
| 462 | def resize( |
| 463 | self, |
| 464 | image: PIL.Image.Image | np.ndarray | torch.Tensor, |
| 465 | height: int, |
| 466 | width: int, |
| 467 | resize_mode: str = "default", # "default", "fill", "crop" |
| 468 | ) -> PIL.Image.Image | np.ndarray | torch.Tensor: |
| 469 | """ |
| 470 | Resize image. |
| 471 | |
| 472 | Args: |
| 473 | image (`PIL.Image.Image`, `np.ndarray` or `torch.Tensor`): |
| 474 | The image input, can be a PIL image, numpy array or pytorch tensor. |
| 475 | height (`int`): |
| 476 | The height to resize to. |
| 477 | width (`int`): |
| 478 | The width to resize to. |
| 479 | resize_mode (`str`, *optional*, defaults to `default`): |
| 480 | The resize mode to use, can be one of `default` or `fill`. If `default`, will resize the image to fit |
| 481 | within the specified width and height, and it may not maintaining the original aspect ratio. If `fill`, |
| 482 | will resize the image to fit within the specified width and height, maintaining the aspect ratio, and |
| 483 | then center the image within the dimensions, filling empty with data from image. If `crop`, will resize |
| 484 | the image to fit within the specified width and height, maintaining the aspect ratio, and then center |
| 485 | the image within the dimensions, cropping the excess. Note that resize_mode `fill` and `crop` are only |
| 486 | supported for PIL image input. |
| 487 | |
| 488 | Returns: |
| 489 | `PIL.Image.Image`, `np.ndarray` or `torch.Tensor`: |
| 490 | The resized image. |
| 491 | """ |
| 492 | if resize_mode != "default" and not isinstance(image, PIL.Image.Image): |
| 493 | raise ValueError(f"Only PIL image input is supported for resize_mode {resize_mode}") |
| 494 | if isinstance(image, PIL.Image.Image): |
| 495 | if resize_mode == "default": |
| 496 | image = image.resize( |
| 497 | (width, height), |
| 498 | resample=PIL_INTERPOLATION[self.config.resample], |
| 499 | reducing_gap=self.config.reducing_gap, |
| 500 | ) |
| 501 | elif resize_mode == "fill": |
| 502 | image = self._resize_and_fill(image, width, height) |
| 503 | elif resize_mode == "crop": |
| 504 | image = self._resize_and_crop(image, width, height) |
| 505 | else: |
| 506 | raise ValueError(f"resize_mode {resize_mode} is not supported") |
| 507 | |
| 508 | elif isinstance(image, torch.Tensor): |
| 509 | image = torch.nn.functional.interpolate( |
| 510 | image, |
| 511 | size=(height, width), |
| 512 | ) |
| 513 | elif isinstance(image, np.ndarray): |
| 514 | image = self.numpy_to_pt(image) |
| 515 | image = torch.nn.functional.interpolate( |
| 516 | image, |
| 517 | size=(height, width), |
| 518 | ) |
| 519 | image = self.pt_to_numpy(image) |