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

Method preprocess

src/diffusers/image_processor.py:468–594  ·  view source on GitHub ↗

Preprocess the image input. Args: image (`pipeline_image_input`): The image input, accepted formats are PIL images, NumPy arrays, PyTorch tensors; Also accept list of supported formats. height (`int`, *optional*, defaults to `

(
        self,
        image: PipelineImageInput,
        height: Optional[int] = None,
        width: Optional[int] = None,
        resize_mode: str = "default",  # "default", "fill", "crop"
        crops_coords: Optional[Tuple[int, int, int, int]] = None,
    )

Source from the content-addressed store, hash-verified

466 return height, width
467
468 def preprocess(
469 self,
470 image: PipelineImageInput,
471 height: Optional[int] = None,
472 width: Optional[int] = None,
473 resize_mode: str = "default", # "default", "fill", "crop"
474 crops_coords: Optional[Tuple[int, int, int, int]] = None,
475 ) -> torch.Tensor:
476 """
477 Preprocess the image input.
478
479 Args:
480 image (`pipeline_image_input`):
481 The image input, accepted formats are PIL images, NumPy arrays, PyTorch tensors; Also accept list of
482 supported formats.
483 height (`int`, *optional*, defaults to `None`):
484 The height in preprocessed image. If `None`, will use the `get_default_height_width()` to get default
485 height.
486 width (`int`, *optional*`, defaults to `None`):
487 The width in preprocessed. If `None`, will use get_default_height_width()` to get the default width.
488 resize_mode (`str`, *optional*, defaults to `default`):
489 The resize mode, can be one of `default` or `fill`. If `default`, will resize the image to fit within
490 the specified width and height, and it may not maintaining the original aspect ratio. If `fill`, will
491 resize the image to fit within the specified width and height, maintaining the aspect ratio, and then
492 center the image within the dimensions, filling empty with data from image. If `crop`, will resize the
493 image to fit within the specified width and height, maintaining the aspect ratio, and then center the
494 image within the dimensions, cropping the excess. Note that resize_mode `fill` and `crop` are only
495 supported for PIL image input.
496 crops_coords (`List[Tuple[int, int, int, int]]`, *optional*, defaults to `None`):
497 The crop coordinates for each image in the batch. If `None`, will not crop the image.
498 """
499 supported_formats = (PIL.Image.Image, np.ndarray, torch.Tensor)
500
501 # Expand the missing dimension for 3-dimensional pytorch tensor or numpy array that represents grayscale image
502 if self.config.do_convert_grayscale and isinstance(image, (torch.Tensor, np.ndarray)) and image.ndim == 3:
503 if isinstance(image, torch.Tensor):
504 # if image is a pytorch tensor could have 2 possible shapes:
505 # 1. batch x height x width: we should insert the channel dimension at position 1
506 # 2. channel x height x width: we should insert batch dimension at position 0,
507 # however, since both channel and batch dimension has same size 1, it is same to insert at position 1
508 # for simplicity, we insert a dimension of size 1 at position 1 for both cases
509 image = image.unsqueeze(1)
510 else:
511 # if it is a numpy array, it could have 2 possible shapes:
512 # 1. batch x height x width: insert channel dimension on last position
513 # 2. height x width x channel: insert batch dimension on first position
514 if image.shape[-1] == 1:
515 image = np.expand_dims(image, axis=0)
516 else:
517 image = np.expand_dims(image, axis=-1)
518
519 if isinstance(image, list) and isinstance(image[0], np.ndarray) and image[0].ndim == 4:
520 warnings.warn(
521 "Passing `image` as a list of 4d np.ndarray is deprecated."
522 "Please concatenate the list along the batch dimension and pass it as a single 4d np.ndarray",
523 FutureWarning,
524 )
525 image = np.concatenate(image, axis=0)

Calls 10

resizeMethod · 0.95
convert_to_rgbMethod · 0.95
convert_to_grayscaleMethod · 0.95
pil_to_numpyMethod · 0.95
numpy_to_ptMethod · 0.95
normalizeMethod · 0.95
binarizeMethod · 0.95
is_valid_image_imagelistFunction · 0.85
cropMethod · 0.45