The Wayback Machine - https://web.archive.org/web/20201007210239/https://github.com/TheAlgorithms/Python/pull/2946/files
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added computer vision algorithm #2946

Open
wants to merge 3 commits into
base: master
from
@@ -0,0 +1,34 @@
from PIL import Image

"""
Mean thresholding algorithm for image processing
https://en.wikipedia.org/wiki/Thresholding_(image_processing)
"""


def mean_threshold(image: Image) -> Image:
"""
image: is a grayscale PIL image object
"""
height, width = image.size
mean = 0
pixels = image.load()
for i in range(width):
for j in range(height):
pixel = pixels[j, i]
mean += pixel
mean //= width * height

for j in range(width):
for i in range(height):
if pixels[i, j] > mean:
pixels[i, j] = 255
else:
pixels[i, j] = 0
return image


if __name__ == "__main__":
image = Image.open("path_to_image").convert("L")
image = mean_threshold(image)
image.save("output_image_path")
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.