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

Class VaeImageProcessor

src/diffusers/image_processor.py:60–684  ·  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

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

Calls

no outgoing calls