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"
)
| 328 | return res |
| 329 | |
| 330 | def resize( |
| 331 | self, |
| 332 | image: Union[PIL.Image.Image, np.ndarray, torch.Tensor], |
| 333 | height: int, |
| 334 | width: int, |
| 335 | resize_mode: str = "default", # "default", "fill", "crop" |
| 336 | ) -> Union[PIL.Image.Image, np.ndarray, torch.Tensor]: |
| 337 | """ |
| 338 | Resize image. |
| 339 | |
| 340 | Args: |
| 341 | image (`PIL.Image.Image`, `np.ndarray` or `torch.Tensor`): |
| 342 | The image input, can be a PIL image, numpy array or pytorch tensor. |
| 343 | height (`int`): |
| 344 | The height to resize to. |
| 345 | width (`int`): |
| 346 | The width to resize to. |
| 347 | resize_mode (`str`, *optional*, defaults to `default`): |
| 348 | The resize mode to use, can be one of `default` or `fill`. If `default`, will resize the image to fit |
| 349 | within the specified width and height, and it may not maintaining the original aspect ratio. |
| 350 | If `fill`, will resize the image to fit within the specified width and height, maintaining the aspect ratio, and then center the image |
| 351 | within the dimensions, filling empty with data from image. |
| 352 | If `crop`, will resize the image to fit within the specified width and height, maintaining the aspect ratio, and then center the image |
| 353 | within the dimensions, cropping the excess. |
| 354 | Note that resize_mode `fill` and `crop` are only supported for PIL image input. |
| 355 | |
| 356 | Returns: |
| 357 | `PIL.Image.Image`, `np.ndarray` or `torch.Tensor`: |
| 358 | The resized image. |
| 359 | """ |
| 360 | if resize_mode != "default" and not isinstance(image, PIL.Image.Image): |
| 361 | raise ValueError(f"Only PIL image input is supported for resize_mode {resize_mode}") |
| 362 | if isinstance(image, PIL.Image.Image): |
| 363 | if resize_mode == "default": |
| 364 | image = image.resize((width, height), resample=PIL_INTERPOLATION[self.config.resample]) |
| 365 | elif resize_mode == "fill": |
| 366 | image = self._resize_and_fill(image, width, height) |
| 367 | elif resize_mode == "crop": |
| 368 | image = self._resize_and_crop(image, width, height) |
| 369 | else: |
| 370 | raise ValueError(f"resize_mode {resize_mode} is not supported") |
| 371 | |
| 372 | elif isinstance(image, torch.Tensor): |
| 373 | image = torch.nn.functional.interpolate( |
| 374 | image, |
| 375 | size=(height, width), |
| 376 | ) |
| 377 | elif isinstance(image, np.ndarray): |
| 378 | image = self.numpy_to_pt(image) |
| 379 | image = torch.nn.functional.interpolate( |
| 380 | image, |
| 381 | size=(height, width), |
| 382 | ) |
| 383 | image = self.pt_to_numpy(image) |
| 384 | return image |
| 385 | |
| 386 | def binarize(self, image: PIL.Image.Image) -> PIL.Image.Image: |
| 387 | """ |