Reading Image as Color or GrayScale Image in Python using OpenCV - Machine Learning Tutorials, Courses and Certifications (2024)

Machine Learning April 4, 2021Computer VisionLeave a comment7,625 Views

Related Articles

2 – Read Color and GrayScale Image

Reading Image as Color or GrayScale Image in Python using OpenCV

In this article, we’ll try to open an image by using OpenCV (Open Source Computer Vision). To use the OpenCV library in python, we need to install these libraries as a prerequisite:

  • Numpy Library (Necessary, because OpenCV uses it in the background).
  • OpenCV python

To install these libraries, we need to run these pip commands in cmd:

  • pip install opencv-python
  • pip install numpy
  • pip install matplotlib

Functions you need to understand

  • Read an image from file (using cv::imread)
  • Display an image in an OpenCV window (using cv::imshow)
  • Write an image to a file (using cv::imwrite)

Reading Image as Color or GrayScale Image in Python using OpenCV - Machine Learning Tutorials, Courses and Certifications (1)Reading Image as Color or GrayScale Image in Python using OpenCV - Machine Learning Tutorials, Courses and Certifications (2)

Reading a GrayScale Image in Python using OpenCV

In[5]:

import cv2img = cv2.imread('machinelearning.png',cv2.IMREAD_GRAYSCALE)cv2.imshow('Machine Learning',img)cv2.waitKey(0)cv2.destroyAllWindows()

Note: Instead of writng cv2.IMREAD_GRAYSCALE, write 0 to read image as GrayScale imageReading Image as Color or GrayScale Image in Python using OpenCV - Machine Learning Tutorials, Courses and Certifications (3)

In[2]:

import cv2img = cv2.imread('machinelearning.png',0)cv2.imshow('Machine Learning',img)cv2.waitKey(0)cv2.destroyAllWindows()

Reading a Color Image in Python using OpenCV

In[3]:

import cv2img = cv2.imread('machinelearning.png',cv2.IMREAD_COLOR)cv2.imshow('Machine Learning',img)cv2.waitKey(0)cv2.destroyAllWindows()

Note: Instead of writng cv2.IMREAD_COLOR, write 1 to read image as color image

Reading Image as Color or GrayScale Image in Python using OpenCV - Machine Learning Tutorials, Courses and Certifications (4)

In[4]:

import cv2img = cv2.imread('machinelearning.png',1)cv2.imshow('Machine Learning',img)cv2.waitKey(0)cv2.destroyAllWindows()

Related

Check Also

Record Video From a Webcam using OpenCV

12 Record Video using OpenCV Record Video From a Webcam using OpenCV¶ cv2.VideoWriter( filename, fourcc, …

Reading Image as Color or GrayScale Image in Python using OpenCV - Machine Learning Tutorials, Courses and Certifications (2024)

FAQs

How to read grayscale image in OpenCV Python? ›

Syntax of OpenCV cv2. imread() Method
  1. IMREAD_COLOR – It specifies to load a color image. Any transparency of image will be neglected. ...
  2. IMREAD_GRAYSCALE – It specifies to load an image in grayscale mode. ...
  3. IMREAD_UNCHANGED – It specifies to load an image as such including alpha channel.
May 30, 2023

How to convert color image to grayscale in OpenCV? ›

To convert an RGB image to grayscale using OpenCV, we can use the cv2. cvtColor() function and specify the conversion code as cv2. COLOR_BGR2GRAY. This will convert the image to grayscale and return a single-channel image.

How to convert grayscale image to color image in Python? ›

PIL is a popular library used for image manipulation and processing in Python. To convert a grayscale image to color, we can use the PIL ImageOps module's colorize function, which applies a color tint to a grayscale image based on the specified color and intensity values.

How to read a color image in Python? ›

In the above code, cv2. imread() function is used to read the image in color mode and store it in the input_image variable. Then, the cv2. split() function is used to split the image into its three color channels (blue, green, and red), which are stored in blue, green, and red variables respectively.

Why do we use grayscale images in image processing? ›

It helps in simplifying algorithms and as well eliminates the complexities related to computational requirements. It makes room for easier learning for those who are new to image processing. This is because grayscale compressors an image to its barest minimum pixel. It enhances easy visualisation.

How do you display grayscale image in Python? ›

Displaying Grayscale image

Now open the image using PIL image method and convert it to L mode If you have an L mode image, that means it is a single-channel image – normally interpreted as grayscale. It only stores a grayscale, not color. Plotting the image as cmap = 'gray' converts the colors.

What is the algorithm to convert color to grayscale? ›

The RGB values are converted to grayscale using the NTSC formula: 0.299 ∙ Red + 0.587 ∙ Green + 0.114 ∙ Blue. This formula closely represents the average person's relative perception of the brightness of red, green, and blue light.

How do I know if an image is RGB or grayscale in Python? ›

Simply do print(img. shape) and see how its output is. If it's like something (256,256,3) then it's an RGB image where 3 represents 3 channels. But if it's something like (256,256) then it's a grayscale image.

How to read images in Python using OpenCV? ›

GETTING STARTED (HOW TO READ IMAGES)
  1. Open PyCharm.
  2. Import cv2.
  3. Paste a test image in the directory.
  4. Create variable to store image using imread() function.
  5. Display the image using imshow() function.
  6. Add a delay using a waitkey() function.
Dec 11, 2020

Which image colour format read from OpenCV? ›

The image should be in the working directory or a full path of image should be given. By default, OpenCV stores colored images in BGR(Blue Green and Red) format.

How to read an image in grayscale? ›

Method 1: Using imread() function

So to convert the color image to grayscale we will be usingcv2. imread("image-name. png",0)or you can also writecv2. IMREAD_GRAYSCALEin the place of 0 as it also denotes the same constant.

How to convert grayscale image to binary image in Python OpenCV? ›

We use cv2. threshold() to convert a grayscale image to binary image. To convert a color image to binary image, we first convert the color image to grayscale image using cv2. cvtColor() then apply cv2.

How to read png image in Python using cv2? ›

GETTING STARTED (HOW TO READ IMAGES)
  1. Open PyCharm.
  2. Import cv2.
  3. Paste a test image in the directory.
  4. Create variable to store image using imread() function.
  5. Display the image using imshow() function.
  6. Add a delay using a waitkey() function.
Dec 11, 2020

How to read grayscale image in OpenCV C++? ›

Reading and Displaying an image in OpenCV using C++
  1. IMREAD_COLOR: The default mode in which the image is loaded if no arguments are provided. It loads the image in BGR format.
  2. IMREAD_UNCHANGED: It loads the image in the original form. ...
  3. IMREAD_GRAYSCALE: It loads the image as a grayscale image.
Jan 13, 2021

Top Articles
Latest Posts
Article information

Author: Corie Satterfield

Last Updated:

Views: 6334

Rating: 4.1 / 5 (42 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Corie Satterfield

Birthday: 1992-08-19

Address: 850 Benjamin Bridge, Dickinsonchester, CO 68572-0542

Phone: +26813599986666

Job: Sales Manager

Hobby: Table tennis, Soapmaking, Flower arranging, amateur radio, Rock climbing, scrapbook, Horseback riding

Introduction: My name is Corie Satterfield, I am a fancy, perfect, spotless, quaint, fantastic, funny, lucky person who loves writing and wants to share my knowledge and understanding with you.