r""" Convert a NumPy image or a batch of images to a list of PIL images. Args: images (`np.ndarray`): The input NumPy array of images, which can be a single image or a batch. Returns: `list[PIL.Image.Image]`: A list of
(images: np.ndarray)
| 993 | |
| 994 | @staticmethod |
| 995 | def numpy_to_pil(images: np.ndarray) -> list[PIL.Image.Image]: |
| 996 | r""" |
| 997 | Convert a NumPy image or a batch of images to a list of PIL images. |
| 998 | |
| 999 | Args: |
| 1000 | images (`np.ndarray`): |
| 1001 | The input NumPy array of images, which can be a single image or a batch. |
| 1002 | |
| 1003 | Returns: |
| 1004 | `list[PIL.Image.Image]`: |
| 1005 | A list of PIL images converted from the input NumPy array. |
| 1006 | """ |
| 1007 | if images.ndim == 3: |
| 1008 | images = images[None, ...] |
| 1009 | images = (images * 255).round().astype("uint8") |
| 1010 | if images.shape[-1] == 1: |
| 1011 | # special case for grayscale (single channel) images |
| 1012 | pil_images = [Image.fromarray(image.squeeze(), mode="L") for image in images] |
| 1013 | else: |
| 1014 | pil_images = [Image.fromarray(image[:, :, :3]) for image in images] |
| 1015 | |
| 1016 | return pil_images |
| 1017 | |
| 1018 | @staticmethod |
| 1019 | def depth_pil_to_numpy(images: list[PIL.Image.Image] | PIL.Image.Image) -> np.ndarray: |