Now, we will pass another argument, which is called –preprocess whose value is one of the following two. Until now, at the time of the command, we have passed only one argument called –image. Mine image is the right side of the following output. You can check your current project folder and look for an image name like 4999.png or any four digits number.png image. Now, go to the terminal and type the following command with the image path. # Fetch the arguments from the command line # Function to convert RGB Image to GrayScale
So, our final code looks like the following. Here, the data.jpg image is passed to the convertImageToGray() method. So, when we run the command python3 app.py, we pass the argument path to the image like the following. –image: The path to the image we’re sending to the function.We have the following command-line arguments: # Fetch the arguments from the command lineĪp.add_argument("-i", "-image", required=True, Step 3: Fetch the input image.Īdd the following code inside the app.py file.
Since we want to convert the original RGB image from the BGR color space to gray, we use the code COLOR_BGR2GRAY. As a second input, it gets the color space conversion code. To do it, we have to call the cvtColor() function, which allows us to convert the image from a color space to another.Īs the first input, this function receives the original image. Next, we need to parse the image to grayscale. This parameter contains the image we need to convert to grayscale.Īs an additional note, which will be necessary for the conversion to grayscale, the imread() function has the channels stored in BGR (Blue, Green, and Red) order by default. You can see that we have defined a function called convertImageToGray() method. Print('The image is converted to Grayscale successfully') Gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) Nonetheless, for robust code implementation, you should handle these types of exceptions. You can read the original ITU-R Recommendation 709 6th edition.For simplicity, we assume that the file exists, and everything loads fine, so we will not be doing any error or exception check. You can read the original ITU-R Recommendation 601 7th edition.
L = R * 299/1000 + G * 587/1000 + B * 114/1000īy iterating through each pixel you can convert 24-bit to 8-bit or 3 channel to 1 channel for each pixel by using the formula above. ITU-R 601 7th Edition Construction of Luminance formula:
One of the standards that can be used is Recommendation 601 from ITU-R (Radiocommunication Sector of International Telecommunication Union or ITU) organization which is also used by pillow library while converting color images to grayscale. So, how do we achieve one value from those three pixel values? We need some kind of averaging. L mode on the other hand only uses one value between 0-255 for each pixel (8-bit). In summary, color images usually use the RGB format which means every pixel is represented by a tuple of three value (red, green and blue) in Python. There are different image hashes that can be used to transform color images to grayscale.