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: Union[PIL.Image.Image, np.ndarray, torch.Tensor],
height: int,
width: int,
resize_mode: str = "default", # "default", "fill", "crop"
)
| 351 | return res |
| 352 | |
| 353 | def resize( |
| 354 | self, |
| 355 | image: Union[PIL.Image.Image, np.ndarray, torch.Tensor], |
| 356 | height: int, |
| 357 | width: int, |
| 358 | resize_mode: str = "default", # "default", "fill", "crop" |
| 359 | ) -> Union[PIL.Image.Image, np.ndarray, torch.Tensor]: |
| 360 | """ |
| 361 | Resize image. |
| 362 | |
| 363 | Args: |
| 364 | image (`PIL.Image.Image`, `np.ndarray` or `torch.Tensor`): |
| 365 | The image input, can be a PIL image, numpy array or pytorch tensor. |
| 366 | height (`int`): |
| 367 | The height to resize to. |
| 368 | width (`int`): |
| 369 | The width to resize to. |
| 370 | resize_mode (`str`, *optional*, defaults to `default`): |
| 371 | The resize mode to use, can be one of `default` or `fill`. If `default`, will resize the image to fit |
| 372 | within the specified width and height, and it may not maintaining the original aspect ratio. If `fill`, |
| 373 | will resize the image to fit within the specified width and height, maintaining the aspect ratio, and |
| 374 | then center the image within the dimensions, filling empty with data from image. If `crop`, will resize |
| 375 | the image to fit within the specified width and height, maintaining the aspect ratio, and then center |
| 376 | the image within the dimensions, cropping the excess. Note that resize_mode `fill` and `crop` are only |
| 377 | supported for PIL image input. |
| 378 | |
| 379 | Returns: |
| 380 | `PIL.Image.Image`, `np.ndarray` or `torch.Tensor`: |
| 381 | The resized image. |
| 382 | """ |
| 383 | if resize_mode != "default" and not isinstance(image, PIL.Image.Image): |
| 384 | raise ValueError(f"Only PIL image input is supported for resize_mode {resize_mode}") |
| 385 | if isinstance(image, PIL.Image.Image): |
| 386 | if resize_mode == "default": |
| 387 | image = image.resize((width, height), resample=PIL_INTERPOLATION[self.config.resample]) |
| 388 | elif resize_mode == "fill": |
| 389 | image = self._resize_and_fill(image, width, height) |
| 390 | elif resize_mode == "crop": |
| 391 | image = self._resize_and_crop(image, width, height) |
| 392 | else: |
| 393 | raise ValueError(f"resize_mode {resize_mode} is not supported") |
| 394 | |
| 395 | elif isinstance(image, torch.Tensor): |
| 396 | image = torch.nn.functional.interpolate( |
| 397 | image, |
| 398 | size=(height, width), |
| 399 | ) |
| 400 | elif isinstance(image, np.ndarray): |
| 401 | image = self.numpy_to_pt(image) |
| 402 | image = torch.nn.functional.interpolate( |
| 403 | image, |
| 404 | size=(height, width), |
| 405 | ) |
| 406 | image = self.pt_to_numpy(image) |
| 407 | return image |
| 408 | |
| 409 | def binarize(self, image: PIL.Image.Image) -> PIL.Image.Image: |
| 410 | """ |