From 3dd4dad8f69d17da34c9fcdec2fcce4022d6f6f2 Mon Sep 17 00:00:00 2001 From: fieryash Date: Thu, 8 Oct 2020 21:55:10 +0530 Subject: [PATCH 1/4] created a script to convert coloured images to black and white --- image_colour_to_bw/colour_to_bw.py | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 image_colour_to_bw/colour_to_bw.py diff --git a/image_colour_to_bw/colour_to_bw.py b/image_colour_to_bw/colour_to_bw.py new file mode 100644 index 000000000..9a7d033ee --- /dev/null +++ b/image_colour_to_bw/colour_to_bw.py @@ -0,0 +1,10 @@ +import cv2 +def colour_to_bw(original_image_path, output_path): + ''' + Function to convert colour image to black and white + ''' + original_image = cv2.imread(original_image_path) + gray_img = cv2.cvtColor(original_image, cv2.COLOR_BGR2GRAY) + thresh = 128 + img_bw = cv2.threshold(gray_img,thresh,255, cv2.THRESH_BINARY)[1] + cv2.imwrite(output_path+'\final_image.png',img_bw) \ No newline at end of file From 3ea4d49ed7d67abbb8b5c6a548f5145e8ba6a97a Mon Sep 17 00:00:00 2001 From: fieryash Date: Thu, 8 Oct 2020 22:33:16 +0530 Subject: [PATCH 2/4] added Readme.md and requirements.txt --- image_colour_to_bw/README.md | 16 ++++++++++++++++ image_colour_to_bw/colour_to_bw.py | 2 +- image_colour_to_bw/requirements.txt | 1 + 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 image_colour_to_bw/README.md create mode 100644 image_colour_to_bw/requirements.txt diff --git a/image_colour_to_bw/README.md b/image_colour_to_bw/README.md new file mode 100644 index 000000000..d58e52b2f --- /dev/null +++ b/image_colour_to_bw/README.md @@ -0,0 +1,16 @@ +# Script to convert coloured images to black and white +## How to use: +1. Make the directory structure as shown below: +
. +
├── colour_to_bw.py +
└── image.png + +2. Call the function giving two arguments, the path to the original image and the path for your output image +
example :- +
```colour_to_bw("./image.png","./")``` + +3. The output of the script will be stored as "final_image.png". The directory structure would look similar to this if you execute the code in step 2. +
. +
├── colour_to_bw.py +
├── final_image.png +
└── ptr_mail.apng diff --git a/image_colour_to_bw/colour_to_bw.py b/image_colour_to_bw/colour_to_bw.py index 9a7d033ee..b0f0e3957 100644 --- a/image_colour_to_bw/colour_to_bw.py +++ b/image_colour_to_bw/colour_to_bw.py @@ -7,4 +7,4 @@ def colour_to_bw(original_image_path, output_path): gray_img = cv2.cvtColor(original_image, cv2.COLOR_BGR2GRAY) thresh = 128 img_bw = cv2.threshold(gray_img,thresh,255, cv2.THRESH_BINARY)[1] - cv2.imwrite(output_path+'\final_image.png',img_bw) \ No newline at end of file + cv2.imwrite(output_path+'final_image.png',img_bw) \ No newline at end of file diff --git a/image_colour_to_bw/requirements.txt b/image_colour_to_bw/requirements.txt new file mode 100644 index 000000000..1db7aea11 --- /dev/null +++ b/image_colour_to_bw/requirements.txt @@ -0,0 +1 @@ +opencv-python \ No newline at end of file From a74f8c2aec483b255cb12be25686dedf5d67ab1e Mon Sep 17 00:00:00 2001 From: fieryash Date: Thu, 8 Oct 2020 22:43:17 +0530 Subject: [PATCH 3/4] fixed linting issues --- image_colour_to_bw/colour_to_bw.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/image_colour_to_bw/colour_to_bw.py b/image_colour_to_bw/colour_to_bw.py index b0f0e3957..c77e4d297 100644 --- a/image_colour_to_bw/colour_to_bw.py +++ b/image_colour_to_bw/colour_to_bw.py @@ -1,4 +1,6 @@ import cv2 + + def colour_to_bw(original_image_path, output_path): ''' Function to convert colour image to black and white @@ -6,5 +8,5 @@ def colour_to_bw(original_image_path, output_path): original_image = cv2.imread(original_image_path) gray_img = cv2.cvtColor(original_image, cv2.COLOR_BGR2GRAY) thresh = 128 - img_bw = cv2.threshold(gray_img,thresh,255, cv2.THRESH_BINARY)[1] - cv2.imwrite(output_path+'final_image.png',img_bw) \ No newline at end of file + img_bw = cv2.threshold(gray_img, thresh, 255, cv2.THRESH_BINARY)[1] + cv2.imwrite(output_path+'final_image.png', img_bw) From 8ead2c6dd5cd6cd76c522ecbe2d38370dfbc348f Mon Sep 17 00:00:00 2001 From: fieryash Date: Thu, 8 Oct 2020 22:47:02 +0530 Subject: [PATCH 4/4] fixed arithmetic operator linting --- image_colour_to_bw/colour_to_bw.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/image_colour_to_bw/colour_to_bw.py b/image_colour_to_bw/colour_to_bw.py index c77e4d297..fd744d262 100644 --- a/image_colour_to_bw/colour_to_bw.py +++ b/image_colour_to_bw/colour_to_bw.py @@ -9,4 +9,4 @@ def colour_to_bw(original_image_path, output_path): gray_img = cv2.cvtColor(original_image, cv2.COLOR_BGR2GRAY) thresh = 128 img_bw = cv2.threshold(gray_img, thresh, 255, cv2.THRESH_BINARY)[1] - cv2.imwrite(output_path+'final_image.png', img_bw) + cv2.imwrite(output_path + 'final_image.png', img_bw)