r""" Convert a numpy image or a batch of images to a PIL image. Args: images (`np.ndarray`): The image array to convert to PIL format. Returns: `list[PIL.Image.Image]`: A list of PIL images.
(images: np.ndarray)
| 126 | |
| 127 | @staticmethod |
| 128 | def numpy_to_pil(images: np.ndarray) -> list[PIL.Image.Image]: |
| 129 | r""" |
| 130 | Convert a numpy image or a batch of images to a PIL image. |
| 131 | |
| 132 | Args: |
| 133 | images (`np.ndarray`): |
| 134 | The image array to convert to PIL format. |
| 135 | |
| 136 | Returns: |
| 137 | `list[PIL.Image.Image]`: |
| 138 | A list of PIL images. |
| 139 | """ |
| 140 | if images.ndim == 3: |
| 141 | images = images[None, ...] |
| 142 | images = (images * 255).round().astype("uint8") |
| 143 | if images.shape[-1] == 1: |
| 144 | # special case for grayscale (single channel) images |
| 145 | pil_images = [Image.fromarray(image.squeeze(), mode="L") for image in images] |
| 146 | else: |
| 147 | pil_images = [Image.fromarray(image) for image in images] |
| 148 | |
| 149 | return pil_images |
| 150 | |
| 151 | @staticmethod |
| 152 | def pil_to_numpy(images: list[PIL.Image.Image] | PIL.Image.Image) -> np.ndarray: |
no outgoing calls