MCPcopy Create free account
hub / github.com/Chen-Yang-Liu/Text2Earth / resize

Method resize

src/diffusers/image_processor.py:352–406  ·  view source on GitHub ↗

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"
    )

Source from the content-addressed store, hash-verified

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

Callers 15

preprocessMethod · 0.95
apply_overlayMethod · 0.95
_resize_and_fillMethod · 0.45
_resize_and_cropMethod · 0.45
preprocessMethod · 0.45
get_inputsMethod · 0.45
get_inputsMethod · 0.45
get_inputsMethod · 0.45
get_dummy_inputsMethod · 0.45

Calls 5

_resize_and_fillMethod · 0.95
_resize_and_cropMethod · 0.95
numpy_to_ptMethod · 0.95
pt_to_numpyMethod · 0.95
interpolateMethod · 0.45

Tested by 15

get_inputsMethod · 0.36
get_inputsMethod · 0.36
get_inputsMethod · 0.36
get_dummy_inputsMethod · 0.36
get_dummy_inputsMethod · 0.36
get_dummy_inputsMethod · 0.36
setUpClassMethod · 0.36