Python makes it easy to import images and play around with them. Knowing how to read images in Python will enable you to do image processing and train machine learning models on image data.
What is Image Processing?
Image processing involves performing some operations on an image, in order to get an enhanced image or to extract some useful information from it.
The field of image processing is upcoming and advancing rapidly. It enables object detection in images that has applications ranging from self driving cars to tumor detection in the field of medical science.
What are images?
This is an absurd question. Of course, you know what an image is. Rather a better question to ask would be ‘what is an image for a machine’.
What you see as an image is actually a 2D matrix for computer.
A digital image is stored as a combination of pixels. Each pixel further contains a different number of channels. If it a grayscale image, it has only one pixel, whereas a colored image contains three channels: red, green, and blue.
Each channel of each pixel has a value between 0 and 255. With the combination of red, green and blue in different proportions we can create any colour.
Using OpenCV to Read Images in Python
In this tutorial, we will learn how to read images in Python using the OpenCV library.
OpenCV is an open-source computer vision and machine learning software library of programming functions mainly aimed at real-time computer vision.
1. Install the OpenCV library
You can install the package using the pip command as below:
pip install opencv-python
To use OpenCV in your Python project you will need to import it. To import it use the following line:
2. Programming to Read images
To read an image using OpenCV, use the following line of code.
img = cv2.imread('image_path')
Now the variable img will be a matrix of pixel values. We can print it and see the RGB values.
The image we are using for this example is:
To print the matrix use:
3. Complete Implementation to Read Images in Python using OpenCV
The complete code is as follows:
import cv2 #read img = cv2.imread('sample_image.jpg') #show print(img)
Display Images Using Python OpenCV
To show the image using OpenCV use the following line:
ccv2.imshow('image',img) cv2.waitKey(0) cv2.destroyAllWindows()
cv2.waitKey() is a keyboard binding function. Its argument is the time in milliseconds.
The function waits for specified milliseconds for any keyboard event. If you press any key in that time, the program continues. If 0 is passed, it waits indefinitely for a keystroke.
The complete code for displaying the image is:
import cv2 #read img = cv2.imread('sample_image.jpg') #show cv2.imshow('image',img) cv2.waitKey(0) cv2.destroyAllWindows()
Manipulating images using Python OpenCV
There are a lot of functionalities in OpenCV that allow you to manipulate an image. We will look at how to turn an image into grayscale.
A grayscale image means that each pixel will only have one channel with values between 0 to 255.
The code to do that is :
gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
Implementing the Python code
The complete code for turning an image to grayscale is :
#read img = cv2.imread('sample_image.jpg') #to grayscale gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #show print(gray_image) cv2.imshow('image',gray_image) cv2.waitKey(0) cv2.destroyAllWindows()
You can see that the dimension of this matrix is different from that of a colored image.
Save the manipulated image file
To save an image after manipulation use the following line of code:
Here, the first argument is the name you want to give to the file, the second argument is the variable that contains the image you want to save. We are saving the grayscale image we created above.
Complete Python code for turning a color image greyscale
The complete code for saving an image is:
import cv2 #read img = cv2.imread('sample_image.jpg') #to grayscale gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #show print(gray_image) #save cv2.imwrite('sample_grayscale.jpg',gray_image)
In this tutorial we covered how to read and manipulate images in Python using OpenCV. To further explore OpenCV read its documentation.