Convert a numpy image or a batch of images to a PIL image.
(images)
| 33 | |
| 34 | |
| 35 | def numpy_to_pil(images): |
| 36 | """ |
| 37 | Convert a numpy image or a batch of images to a PIL image. |
| 38 | """ |
| 39 | if images.ndim == 3: |
| 40 | images = images[None, ...] |
| 41 | images = (images * 255).round().astype("uint8") |
| 42 | if images.shape[-1] == 1: |
| 43 | # special case for grayscale (single channel) images |
| 44 | pil_images = [Image.fromarray(image.squeeze(), mode="L") for image in images] |
| 45 | else: |
| 46 | pil_images = [Image.fromarray(image) for image in images] |
| 47 | |
| 48 | return pil_images |
| 49 | |
| 50 | |
| 51 | 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
searching dependent graphs…