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