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
| 25 | |
| 26 | |
| 27 | class VaeImageProcessor(ConfigMixin): |
| 28 | """ |
| 29 | Image processor for VAE. |
| 30 | |
| 31 | Args: |
| 32 | do_resize (`bool`, *optional*, defaults to `True`): |
| 33 | Whether to downscale the image's (height, width) dimensions to multiples of `vae_scale_factor`. Can accept |
| 34 | `height` and `width` arguments from [`image_processor.VaeImageProcessor.preprocess`] method. |
| 35 | vae_scale_factor (`int`, *optional*, defaults to `8`): |
| 36 | VAE scale factor. If `do_resize` is `True`, the image is automatically resized to multiples of this factor. |
| 37 | resample (`str`, *optional*, defaults to `lanczos`): |
| 38 | Resampling filter to use when resizing the image. |
| 39 | do_normalize (`bool`, *optional*, defaults to `True`): |
| 40 | Whether to normalize the image to [-1,1]. |
| 41 | do_convert_rgb (`bool`, *optional*, defaults to be `False`): |
| 42 | Whether to convert the images to RGB format. |
| 43 | """ |
| 44 | |
| 45 | config_name = CONFIG_NAME |
| 46 | |
| 47 | @register_to_config |
| 48 | def __init__( |
| 49 | self, |
| 50 | do_resize: bool = True, |
| 51 | vae_scale_factor: int = 8, |
| 52 | resample: str = "lanczos", |
| 53 | do_normalize: bool = True, |
| 54 | do_convert_rgb: bool = False, |
| 55 | ): |
| 56 | super().__init__() |
| 57 | |
| 58 | @staticmethod |
| 59 | def numpy_to_pil(images: np.ndarray) -> PIL.Image.Image: |
| 60 | """ |
| 61 | Convert a numpy image or a batch of images to a PIL image. |
| 62 | """ |
| 63 | if images.ndim == 3: |
| 64 | images = images[None, ...] |
| 65 | images = (images * 255).round().astype("uint8") |
| 66 | if images.shape[-1] == 1: |
| 67 | # special case for grayscale (single channel) images |
| 68 | pil_images = [Image.fromarray(image.squeeze(), mode="L") for image in images] |
| 69 | else: |
| 70 | pil_images = [Image.fromarray(image) for image in images] |
| 71 | |
| 72 | return pil_images |
| 73 | |
| 74 | @staticmethod |
| 75 | def pil_to_numpy(images: Union[List[PIL.Image.Image], PIL.Image.Image]) -> np.ndarray: |
| 76 | """ |
| 77 | Convert a PIL image or a list of PIL images to NumPy arrays. |
| 78 | """ |
| 79 | if not isinstance(images, list): |
| 80 | images = [images] |
| 81 | images = [np.array(image).astype(np.float32) / 255.0 for image in images] |
| 82 | images = np.stack(images, axis=0) |
| 83 | |
| 84 | return images |
no outgoing calls