Let’s Learn OpenCV Python: Read, Display & Save an image with OpenCV Python

Welcome to the Tutorial 1 of tutorial series on Let’s Learn OpenCV Python

This post gives the basic tutorial to handle images using OpenCV Python.  We will be reading, displaying and saving them to file. You can follow tutorial on my Youtube channel.

Complete guide to install OpenCV Python (with Numpy & Matplotlib) from scratch.

You can read my post on How to install OpenCV Python on Windows PC. I have a complete video tutorial for that as well.


import cv2# import opencv library
#reading the image
img=cv2.imread('messi.jpg',0)#read the image as grayscale image
#display in screen
k=cv2.waitKey(0) & 0xFF
if k==27:
cv2.destroyAllWindows()#close all display windows
elif k==ord('s'): #wait for 's' to be pressed
#save the image into new file


import cv2

This will import the full OpenCV library into the current kernel, so that we can use the associated functions, variables, classes, etc of the library.



The above line uses imread function, which takes in the path of image as first argument, and a flag as the second argument. The flag signifies the way the image is read by the function. We can pass in the following values of the flag.

Flag nameEnumerateDescription
cv2.IMREAD_COLOR1Reads the color image. Any transparency component is neglected.
cv2.IMREAD_GRAYSCALE0Reads the image as grayscale
cv2.IMREAD_UNCHANGED-1Reads the image 'unchanged' in the sense that even transparency channel is preserved during read operation

Now, that we have read the image in to a Python object named img, let’s display it in a window.

For displaying the img matrix in a window, we use imshow function which takes essentially two arguments; first is the window name, and second is the image matrix.

After displaying the window, we tell Python to wait for the user to press any key using waitKey() function with argument 0, which is the delay for the screen closing. Any non-zero value of delay implies the delay in closing of screen for that much value of milliseconds, whereas 0 implies to wait for indefinitely for the user’s key press. Upon key press, the function returns the ASCII code of the corresponding key.

Note that for certain systems such as x64, the function may return 2-byte code with required ASCII character in the least significant byte. Hence, we just mask out the most significant byte by ANDing it with Hexadecimal number (0xFF, which gives a 8-bit length 1’s).

The next line checks if the key pressed is Esc key, whose ASCII code is 27. Esc keypress invokes the destroyAllWindows() function to close all the windows.

If the user presses key ‘s’, then the image is saved into a new image file with .png format, using imwrite() function whose first argument takes the image file name and second argument is the image object.

The image windows are closed at the end of the program.

Congrats, you have learnt to read, display and save an image in OpenCV Python. You have to go a long way now with the same enthusiasm, and soon you will be able to work upon training your own image detectors, tracking objects and much more.

Thanks. See you soon in the next post. 🙂



You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

16 + 8 =