Introduction :
Hey coders , In the tutorial , we will be learning about how to make an color image black and white in python by grayscale using different method . we will be done this by some simple and easy steps so that it will be easy for us to understand . So , Lets begin .
Methods :
In python to perform this task to make color image black and white we can use two different methods
- Pillow (PIL)
- OpenCV
So , In this tutorial we will convert the color image black and white this by using Pillow (PIL) method .
Pillow (PIL):
Pillow (PIL) is a widely used python library which is used for image processing task . It performs the task directly and this is very good for basic image manipulation or change like converting color image to black and white by grayscale .
Step 1 : Importing the necessary libraries and the image
We will write a line in our program that is ‘ from PIL import Image ‘ this line imports the ‘ Image ‘ class from the Pillow library.
from PIL import Image
Step 2 : Defining the function
Here , We will define the function that is ‘ convert_to_grayscale_pil ‘ which will help us to convert the color image to black and white by grayscale . And also we have to give this 2 parameters according to which it will perform the task . Those parameters are :-
- input_image_path
- output_image_path
The ‘ input_image_path ‘ is a string which represents the path to the input color image .
And the ‘ output_image_path ‘ is a string that represents the path where the grayscale image will be saved after the conversion.
def convert_to_grayscale_pil(input_image_path, output_image_path):
Step 3 : Opening the Image
Now we will write a line that is ‘ image = Image.open(input_image_path) ‘ . Here ‘ Image.open () ‘ is a function of Pillow(PIL) which will open the file that is specified by the ‘image_input_path’ .
And the ‘ image= ‘ assigns the open image to the variable ‘ image ‘ making it accessible for further operations like converting to grayscale and other image manipulations.
image = Image.open(input_image_path)
This step is essential because it prepares the image data so that you can perform tasks such as conversions, saving in different formats, or applying various image processing operations using Pillow.
Step 4 : Converting to Greyscale
Now in this process we will convert the color image to grayscale. To do that we wil write a line in our program that is ‘ grayscale_image = image.convert(“L”) ‘ . Here ‘ image.convert(“L”) ‘ converts the opened colored image (‘image’) to grayscale . And “L” is the argument which specifies the mode according to which the image will be converted . In Pillow, “L” mode stands for luminance, which is grayscale. It means the image will be converted to a single channel representing brightness.
grayscale_image = image.convert("L")
This conversion process effectively reduces the image’s complexity by removing color information, making it easier to analyze or process further depending on the application.
Step 5 : Saving the Image
After the conversion of the image to grayscale we will now save the grayscale image . For that we will have to write a line in our program that is ‘ grayscale_image.save(output_image_path) ‘ . Here ‘ grayscale_image ‘ holds the grayscale version of the original image which is obtained after converting it using ‘ image.convert(“L”) ‘. And ‘ .save(output_image_path) ‘ is the method that saves the ‘ grayscale_image ‘ to the file that is specified by ‘ output_image_path ‘ .
grayscale_image.save(output_image_path)
After converting an image to grayscale and saving it, you’ll have a new file that represents the original image but without colors, making it suitable for applications where color information is not necessary or where grayscale images are preferred.
Step 6 : Showing the confirmation Message
After doing all of the above process , Now we have converted the color image to black and white and now we just have to enter a line that will show us that the process is done and the image is converted successfully for that we just have to add
print(f”Converted {input_image_path} to grayscale. Saved as {output_image_path}“)
which will show us the details that the image is converted and also show us the file location where t is saved after the conversion .
print(f"Converted {input_image_path} to grayscale. Saved as {output_image_path}")
Summary:
- Importing Libraries: Pilow (PIL) is imported to access its image processing capabilities.
- Function Definition: Defines a function ‘ convert_to_grayscale_pil ‘ that takes input and output image paths.
- Opening the Image: Uses ‘ Image.open() ‘ to load the input color image.
- Converting to Grayscale: ‘ image.convert(“L”) ‘ converts the loaded image to grayscale.
- Saving the Image: ‘ grayscale_image.save(output_image_path) ‘ saves the grayscale image to disk.
Full Source Code :
def convert_to_grayscale_pil(input_image_path, output_image_path): image = Image.open(input_image_path) grayscale_image = image.convert("L") grayscale_image.save(output_image_path) print(f"Converted {input_image_path} to grayscale. Saved as {output_image_path}") # Example usage: input_image_path = "color_image.jpg" output_image_path = "grayscale_image.jpg" convert_to_grayscale_pil(input_image_path, output_image_path)