Python cv2 imread: How to Read Image in Python
The cv2.imread() method loads the image from the specified file path. If the image cannot be read (because of the improper permissions, missing file, unsupported or invalid format), then the cv2.imread() method returns an empty matrix.
We will open an image by using OpenCV (Open Source Computer Vision). OpenCV-Python is the library of Python bindings designed to solve computer vision problems.
OpenCV is a library of programming functions mainly aimed at real-time computer vision. Originally developed by Intel, it was later supported by the Willow Garage then Itseez. The library is cross-platform and free for use under the open-source BSD license.
Install Python OpenCV
To work with OpenCV in Python, we have to install the opencv-python module.
Type the following command.
python3 -m pip install opencv-python # OR pip install opencv-python
To use the opencv-python in our project, we have to import the cv2 module in the file.
To read an image using OpenCV in Python, use the cv2.imread() method. The cv2.imread() method loads an image from the specified file.
path: It is a string representing the path of the image to be read.
flag: It specifies the way in which image should be read. Its default value is cv2.IMREAD_COLOR. All three types of flags are described below:
- cv2.IMREAD_COLOR: It defines to load the color image. Any transparency of an image will be neglected. It is a default flag. Alternatively, we can pass integer value 1 for this flag.
- cv2.IMREAD_GRAYSCALE: It defines to load an image in grayscale mode. Alternatively, we can pass integer value 0 for this flag.
- cv2.IMREAD_UNCHANGED: It explains to load an image as such, including the alpha channel. Alternatively, we can pass integer value -1 for this flag.
The method returns an image that is loaded from the specified filesystem.
Example using default flag
Write the following code.
import cv2 path = 'info.png' # Using cv2.imread() method image = cv2.imread(path) # Displaying the image cv2.imshow('image', image) cv2.waitKey(0) cv2.destoryAllWindows()
The cv2.waitKey() function waits for the user to press any key.
The cv2.destroyAllWindows() function closes all open windows.
Loading an image in grayscale mode
If you want to convert RGB image to grayscale image, then this will be helpful.
Write the following code.
import cv2 path = 'info.png' # Using cv2.imread() method image = cv2.imread(path, 0) # Displaying the image cv2.imshow('image', image) cv2.waitKey(0) cv2.destoryAllWindows()
We can pass the flag cv2.IMREAD_UNCHANGED to the cv2.imread() function.
import cv2 path = 'info.png' # Using cv2.imread() method image = cv2.imread(path, cv2.IMREAD_UNCHANGED) # Displaying the image cv2.imshow('image', image) cv2.waitKey(0) cv2.destoryAllWindows()
You can see from the output that the image is unchanged.
When reading a color image file, OpenCV imread() reads as a Numpy array ndarray of row (height) x column (width) x color (3). The order of color is BGR (blue, green, red).
Please one thing to note here is that OpenCV is BGR, Pillow is RGB.
That is it for the Python cv2 imread() method.