MCPcopy Create free account
hub / github.com/TencentARC/BrushNet / preprocess

Method preprocess

src/diffusers/image_processor.py:446–555  ·  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 `None`):

(
        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

444 return height, width
445
446 def preprocess(
447 self,
448 image: PipelineImageInput,
449 height: Optional[int] = None,
450 width: Optional[int] = None,
451 resize_mode: str = "default", # "default", "fill", "crop"
452 crops_coords: Optional[Tuple[int, int, int, int]] = None,
453 ) -> torch.Tensor:
454 """
455 Preprocess the image input.
456
457 Args:
458 image (`pipeline_image_input`):
459 The image input, accepted formats are PIL images, NumPy arrays, PyTorch tensors; Also accept list of supported formats.
460 height (`int`, *optional*, defaults to `None`):
461 The height in preprocessed image. If `None`, will use the `get_default_height_width()` to get default height.
462 width (`int`, *optional*`, defaults to `None`):
463 The width in preprocessed. If `None`, will use get_default_height_width()` to get the default width.
464 resize_mode (`str`, *optional*, defaults to `default`):
465 The resize mode, can be one of `default` or `fill`. If `default`, will resize the image to fit
466 within the specified width and height, and it may not maintaining the original aspect ratio.
467 If `fill`, will resize the image to fit within the specified width and height, maintaining the aspect ratio, and then center the image
468 within the dimensions, filling empty with data from image.
469 If `crop`, will resize the image to fit within the specified width and height, maintaining the aspect ratio, and then center the image
470 within the dimensions, cropping the excess.
471 Note that resize_mode `fill` and `crop` are only supported for PIL image input.
472 crops_coords (`List[Tuple[int, int, int, int]]`, *optional*, defaults to `None`):
473 The crop coordinates for each image in the batch. If `None`, will not crop the image.
474 """
475 supported_formats = (PIL.Image.Image, np.ndarray, torch.Tensor)
476
477 # Expand the missing dimension for 3-dimensional pytorch tensor or numpy array that represents grayscale image
478 if self.config.do_convert_grayscale and isinstance(image, (torch.Tensor, np.ndarray)) and image.ndim == 3:
479 if isinstance(image, torch.Tensor):
480 # if image is a pytorch tensor could have 2 possible shapes:
481 # 1. batch x height x width: we should insert the channel dimension at position 1
482 # 2. channel x height x width: we should insert batch dimension at position 0,
483 # however, since both channel and batch dimension has same size 1, it is same to insert at position 1
484 # for simplicity, we insert a dimension of size 1 at position 1 for both cases
485 image = image.unsqueeze(1)
486 else:
487 # if it is a numpy array, it could have 2 possible shapes:
488 # 1. batch x height x width: insert channel dimension on last position
489 # 2. height x width x channel: insert batch dimension on first position
490 if image.shape[-1] == 1:
491 image = np.expand_dims(image, axis=0)
492 else:
493 image = np.expand_dims(image, axis=-1)
494
495 if isinstance(image, supported_formats):
496 image = [image]
497 elif not (isinstance(image, list) and all(isinstance(i, supported_formats) for i in image)):
498 raise ValueError(
499 f"Input is in incorrect format: {[type(i) for i in image]}. Currently, we only support {', '.join(supported_formats)}"
500 )
501
502 if isinstance(image[0], PIL.Image.Image):
503 if crops_coords is not None:

Calls 9

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
cropMethod · 0.45