Convert a numpy image or a batch of images to a PIL image.
(images)
| 35 | |
| 36 | |
| 37 | def numpy_to_pil(images): |
| 38 | """ |
| 39 | Convert a numpy image or a batch of images to a PIL image. |
| 40 | """ |
| 41 | if images.ndim == 3: |
| 42 | images = images[None, ...] |
| 43 | images = (images * 255).round().astype("uint8") |
| 44 | if images.shape[-1] == 1: |
| 45 | # special case for grayscale (single channel) images |
| 46 | pil_images = [Image.fromarray(image.squeeze(), mode="L") for image in images] |
| 47 | else: |
| 48 | pil_images = [Image.fromarray(image) for image in images] |
| 49 | |
| 50 | return pil_images |
| 51 | |
| 52 | |
| 53 | def make_image_grid(images: List[PIL.Image.Image], rows: int, cols: int, resize: int = None) -> PIL.Image.Image: |
no outgoing calls
no test coverage detected