Convert a NumPy image or a batch of images to a PIL image.
(images: np.ndarray)
| 674 | |
| 675 | @staticmethod |
| 676 | def numpy_to_pil(images: np.ndarray) -> List[PIL.Image.Image]: |
| 677 | """ |
| 678 | Convert a NumPy image or a batch of images to a PIL image. |
| 679 | """ |
| 680 | if images.ndim == 3: |
| 681 | images = images[None, ...] |
| 682 | images = (images * 255).round().astype("uint8") |
| 683 | if images.shape[-1] == 1: |
| 684 | # special case for grayscale (single channel) images |
| 685 | pil_images = [Image.fromarray(image.squeeze(), mode="L") for image in images] |
| 686 | else: |
| 687 | pil_images = [Image.fromarray(image[:, :, :3]) for image in images] |
| 688 | |
| 689 | return pil_images |
| 690 | |
| 691 | @staticmethod |
| 692 | def depth_pil_to_numpy(images: Union[List[PIL.Image.Image], PIL.Image.Image]) -> np.ndarray: |