image: is a grayscale PIL image object
(image: Image)
| 7 | |
| 8 | |
| 9 | def mean_threshold(image: Image) -> Image: |
| 10 | """ |
| 11 | image: is a grayscale PIL image object |
| 12 | """ |
| 13 | height, width = image.size |
| 14 | mean = 0 |
| 15 | pixels = image.load() |
| 16 | for i in range(width): |
| 17 | for j in range(height): |
| 18 | pixel = pixels[j, i] |
| 19 | mean += pixel |
| 20 | mean //= width * height |
| 21 | |
| 22 | for j in range(width): |
| 23 | for i in range(height): |
| 24 | pixels[i, j] = 255 if pixels[i, j] > mean else 0 |
| 25 | return image |
| 26 | |
| 27 | |
| 28 | if __name__ == "__main__": |