Convert a numpy image or a batch of images to a PIL image.
(images: np.ndarray)
| 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 | |
| 99 | return pil_images |
| 100 | |
| 101 | @staticmethod |
| 102 | def pil_to_numpy(images: Union[List[PIL.Image.Image], PIL.Image.Image]) -> np.ndarray: |
no outgoing calls