Convert a numpy image or a batch of images to a PIL image.
(images)
| 54 | |
| 55 | @staticmethod |
| 56 | def numpy_to_pil(images): |
| 57 | """ |
| 58 | Convert a numpy image or a batch of images to a PIL image. |
| 59 | """ |
| 60 | if images.ndim == 3: |
| 61 | images = images[None, ...] |
| 62 | images = (images * 255).round().astype("uint8") |
| 63 | if images.shape[-1] == 1: |
| 64 | # special case for grayscale (single channel) images |
| 65 | pil_images = [Image.fromarray(image.squeeze(), mode="L") for image in images] |
| 66 | else: |
| 67 | pil_images = [Image.fromarray(image) for image in images] |
| 68 | |
| 69 | return pil_images |
| 70 | |
| 71 | @staticmethod |
| 72 | def numpy_to_pt(images): |