From 1b62c0e824a55d754ce803b9c5490706990dfe2b Mon Sep 17 00:00:00 2001 From: Tanay Karve Date: Wed, 7 Oct 2020 00:41:33 +0530 Subject: [PATCH 1/5] Create meanthresholding.py --- computer_vision/meanthresholding.py | 34 +++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 computer_vision/meanthresholding.py diff --git a/computer_vision/meanthresholding.py b/computer_vision/meanthresholding.py new file mode 100644 index 000000000000..c68786f77e70 --- /dev/null +++ b/computer_vision/meanthresholding.py @@ -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: 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") From b0b8532e98a9ab54d597a6be57b592a099c519d7 Mon Sep 17 00:00:00 2001 From: Tanay Karve Date: Wed, 7 Oct 2020 00:43:23 +0530 Subject: [PATCH 2/5] Rename meanthresholding.py to meanthreshold.py --- computer_vision/{meanthresholding.py => meanthreshold.py} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename computer_vision/{meanthresholding.py => meanthreshold.py} (100%) diff --git a/computer_vision/meanthresholding.py b/computer_vision/meanthreshold.py similarity index 100% rename from computer_vision/meanthresholding.py rename to computer_vision/meanthreshold.py From aa9f68f44abf0650ddf61be6cda6979eb93921a7 Mon Sep 17 00:00:00 2001 From: Tanay Karve Date: Wed, 7 Oct 2020 00:58:38 +0530 Subject: [PATCH 3/5] Update meanthreshold.py --- computer_vision/meanthreshold.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/computer_vision/meanthreshold.py b/computer_vision/meanthreshold.py index c68786f77e70..05482882f28b 100644 --- a/computer_vision/meanthreshold.py +++ b/computer_vision/meanthreshold.py @@ -6,7 +6,7 @@ """ -def mean_threshold(image): +def mean_threshold(image: Image) -> Image: """ image: is a grayscale PIL image object """ From 95109c161963f0c76a0098e4a1b6e5502bf04e94 Mon Sep 17 00:00:00 2001 From: Tanay Karve Date: Fri, 16 Oct 2020 18:34:20 +0530 Subject: [PATCH 4/5] Update computer_vision/meanthreshold.py Verified this part works, thanks. Co-authored-by: Christian Clauss --- computer_vision/meanthreshold.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/computer_vision/meanthreshold.py b/computer_vision/meanthreshold.py index 05482882f28b..32270a797206 100644 --- a/computer_vision/meanthreshold.py +++ b/computer_vision/meanthreshold.py @@ -21,10 +21,7 @@ def mean_threshold(image: Image) -> Image: for j in range(width): for i in range(height): - if pixels[i, j] > mean: - pixels[i, j] = 255 - else: - pixels[i, j] = 0 + pixels[i, j] = 255 if pixels[i, j] > mean else 0 return image From a6a866e3c497ff8b4e7e79300d1785ddad39f866 Mon Sep 17 00:00:00 2001 From: Tanay Karve Date: Fri, 16 Oct 2020 18:34:43 +0530 Subject: [PATCH 5/5] Update computer_vision/meanthreshold.py Co-authored-by: Christian Clauss --- computer_vision/meanthreshold.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/computer_vision/meanthreshold.py b/computer_vision/meanthreshold.py index 32270a797206..76657933d6a9 100644 --- a/computer_vision/meanthreshold.py +++ b/computer_vision/meanthreshold.py @@ -26,6 +26,5 @@ def mean_threshold(image: Image) -> Image: if __name__ == "__main__": - image = Image.open("path_to_image").convert("L") - image = mean_threshold(image) + image = mean_threshold(Image.open("path_to_image").convert("L")) image.save("output_image_path")