In this post we are going to discuss how to read an image, how to display it and how to save it back. OpenCV provides three function cv2.imread() , cv2.imshow() , cv2.imwrite() for reading , displaying and saving back an image. Optionally we can also display the images with Matplotlib.
Reading an Image with OpenCV
OpenCV provides the function cv2.imread() to read an image. The image should be in the working directory or a full path of image should be given. Name of image file is the first argument, second argument is a flag which specifies the way image should be read.
- cv2.IMREAD_COLOR Loads a color image. Any transparency of image will be neglected. It is the default flag.
- cv2.IMREAD_GRAYSCALE Loads image in grayscale mode use.
- cv2.IMREAD_UNCHANGED Loads image as such including alpha channel.
Instead of these three flags, we can simply pass integers 1, 0 or -1 respectively.
# import the numpy and OpenCV library import numpy as np import cv2 # Load an image, file should we in current working directory. img = cv2.imread('elaine.tiff')
Even if the image path is wrong, it won’t throw any error, but print(img) will give you None. It is always a best practice to check for None value. So after reading image next part is display the image.
How to Display an Image with OpenCV?
OpenCV provide the function cv2.imshow() to display an image in a window. The window automatically fits to the image size. First argument is a window name which is a string. second argument is our image. You can create as many windows as you wish, but with different window names.
cv2.imshow('image', img) cv2.waitKey(0) cv2.destroyAllWindows()
A screenshot of the window will look like this (in Window machine):
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 we press any key in that time, the program continues. If 0 is passed, it waits indefinitely for a key stroke. It can also be set to detect specific key strokes like, if key a is pressed etc which we will discuss below.
cv2.destroyAllWindows() simply destroys all the windows we created. If we want to destroy any specific window, use the function cv2.destroyWindow() where we pass the exact window name as the argument.
How to save an Image with OpenCV?
To save the image OpenCV provides cv2.imwrite() function. First argument of this function is the file name, second argument is the image you want to save. cv2.imwrite('messigray.png',img) save the image in PNG format in the working directory.
to sum up, below program loads an image, displays it, save the image if you press ‘s’ and exit, or simply exit without saving if you press ESC key.
# import the numpy and OpenCV library import numpy as np import cv2 # Load an image img = cv2.imread('elaine.tiff') cv2.imshow('image', img) k = cv2.waitKey(0) if k == 27: #wait for ESC key to exit cv2.destroyAllWindows() elif k == ord('s'): #wait for 's' key to save and exit cv2.imwrite('elaine.png', img) cv2.destroyAllWindows()View Jupyter Notebook
This article is contributed by Ram Kripal. If you like eLgo Academy and would like to contribute, you can mail your article to email@example.com. See your article appearing on the eLgo Academy page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.