forked from TheAlgorithms/Python
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmean_threshold.py
30 lines (24 loc) · 734 Bytes
/
mean_threshold.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
fromPILimportImage
"""
Mean thresholding algorithm for image processing
https://en.wikipedia.org/wiki/Thresholding_(image_processing)
"""
defmean_threshold(image: Image) ->Image:
"""
image: is a grayscale PIL image object
"""
height, width=image.size
mean=0
pixels=image.load()
foriinrange(width):
forjinrange(height):
pixel=pixels[j, i]
mean+=pixel
mean//=width*height
forjinrange(width):
foriinrange(height):
pixels[i, j] =255ifpixels[i, j] >meanelse0
returnimage
if__name__=="__main__":
image=mean_threshold(Image.open("path_to_image").convert("L"))
image.save("output_image_path")