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

Class VaeImageProcessor

src/diffusers/image_processor.py:41–645  ·  view source on GitHub ↗

Image processor for VAE. Args: do_resize (`bool`, *optional*, defaults to `True`): Whether to downscale the image's (height, width) dimensions to multiples of `vae_scale_factor`. Can accept `height` and `width` arguments from [`image_processor.VaeImageProces

Source from the content-addressed store, hash-verified

39
40
41class VaeImageProcessor(ConfigMixin):
42 """
43 Image processor for VAE.
44
45 Args:
46 do_resize (`bool`, *optional*, defaults to `True`):
47 Whether to downscale the image's (height, width) dimensions to multiples of `vae_scale_factor`. Can accept
48 `height` and `width` arguments from [`image_processor.VaeImageProcessor.preprocess`] method.
49 vae_scale_factor (`int`, *optional*, defaults to `8`):
50 VAE scale factor. If `do_resize` is `True`, the image is automatically resized to multiples of this factor.
51 resample (`str`, *optional*, defaults to `lanczos`):
52 Resampling filter to use when resizing the image.
53 do_normalize (`bool`, *optional*, defaults to `True`):
54 Whether to normalize the image to [-1,1].
55 do_binarize (`bool`, *optional*, defaults to `False`):
56 Whether to binarize the image to 0/1.
57 do_convert_rgb (`bool`, *optional*, defaults to be `False`):
58 Whether to convert the images to RGB format.
59 do_convert_grayscale (`bool`, *optional*, defaults to be `False`):
60 Whether to convert the images to grayscale format.
61 """
62
63 config_name = CONFIG_NAME
64
65 @register_to_config
66 def __init__(
67 self,
68 do_resize: bool = True,
69 vae_scale_factor: int = 8,
70 resample: str = "lanczos",
71 do_normalize: bool = True,
72 do_binarize: bool = False,
73 do_convert_rgb: bool = False,
74 do_convert_grayscale: bool = False,
75 ):
76 super().__init__()
77 if do_convert_rgb and do_convert_grayscale:
78 raise ValueError(
79 "`do_convert_rgb` and `do_convert_grayscale` can not both be set to `True`,"
80 " if you intended to convert the image into RGB format, please set `do_convert_grayscale = False`.",
81 " if you intended to convert the image into grayscale format, please set `do_convert_rgb = False`",
82 )
83 self.config.do_convert_rgb = False
84
85 @staticmethod
86 def numpy_to_pil(images: np.ndarray) -> List[PIL.Image.Image]:
87 """
88 Convert a numpy image or a batch of images to a PIL image.
89 """
90 if images.ndim == 3:
91 images = images[None, ...]
92 images = (images * 255).round().astype("uint8")
93 if images.shape[-1] == 1:
94 # special case for grayscale (single channel) images
95 pil_images = [Image.fromarray(image.squeeze(), mode="L") for image in images]
96 else:
97 pil_images = [Image.fromarray(image) for image in images]
98

Calls

no outgoing calls