Convert the cells into a greyscale PIL.Image.Image and return it to the caller. >>> from random import random >>> cells = [[random() for w in range(31)] for h in range(16)] >>> img = generate_image(cells) >>> isinstance(img, Image.Image) True >>> img.width, img.height
(cells: list[list[int]])
| 42 | |
| 43 | |
| 44 | def generate_image(cells: list[list[int]]) -> Image.Image: |
| 45 | """ |
| 46 | Convert the cells into a greyscale PIL.Image.Image and return it to the caller. |
| 47 | >>> from random import random |
| 48 | >>> cells = [[random() for w in range(31)] for h in range(16)] |
| 49 | >>> img = generate_image(cells) |
| 50 | >>> isinstance(img, Image.Image) |
| 51 | True |
| 52 | >>> img.width, img.height |
| 53 | (31, 16) |
| 54 | """ |
| 55 | # Create the output image |
| 56 | img = Image.new("RGB", (len(cells[0]), len(cells))) |
| 57 | pixels = img.load() |
| 58 | # Generates image |
| 59 | for w in range(img.width): |
| 60 | for h in range(img.height): |
| 61 | color = 255 - int(255 * cells[h][w]) |
| 62 | pixels[w, h] = (color, color, color) |
| 63 | return img |
| 64 | |
| 65 | |
| 66 | if __name__ == "__main__": |