Convert a numpy image or a batch of images to a PIL image.
(images)
| 30 | |
| 31 | |
| 32 | def numpy_to_pil(images): |
| 33 | """ |
| 34 | Convert a numpy image or a batch of images to a PIL image. |
| 35 | """ |
| 36 | if images.ndim == 3: |
| 37 | images = images[None, ...] |
| 38 | images = (images * 255).round().astype("uint8") |
| 39 | if images.shape[-1] == 1: |
| 40 | # special case for grayscale (single channel) images |
| 41 | pil_images = [Image.fromarray(image.squeeze(), mode="L") for image in images] |
| 42 | else: |
| 43 | pil_images = [Image.fromarray(image) for image in images] |
| 44 | |
| 45 | return pil_images |
no outgoing calls
no test coverage detected