Introduction
This tutorial will teach you the basics of working with images using the OpenCV-Python library. By the end of this tutorial, you will be able to read, display, and save images.
Import the required libraries
First, let's import the required libraries. You will use the cv2 library for image processing. Please use import for importing it.
Preparation
Open the Python shell by typing the following command in the terminal at the VM Desktop.
python3
Detail
Input python3 to start the process of python interpreter. Then input the following code.
import cv2
After that, you can use the cv2 library in your code.
Reading an image
To read an image from a file, we use the cv2.imread() function. The function takes the file path as an argument and returns the image as a NumPy array.
Image path is /home/labex/Desktop/Image.jpg
Detail
Input python to start the process of python interpreter. Then input the following code.
## Read the image
image = cv2.imread('/home/labex/Desktop/Image.jpg')
## Check if the image is loaded correctly
if image is None:
print('Error: Image not found.')
else:
print('Image loaded successfully.')
Note
The cv2.imread() function returns None if the image is not found. In this case, we print an error message. Otherwise, we print a success message.
Displaying the image
To display the image, we use the cv2.imshow()function. The function takes two arguments: the window name and the image to display.
Also, we use the cv2.waitKey() function to wait for a key press. This is required to keep the window open until the user presses a key. The cv2.waitKey() function takes a single argument, which is the number of milliseconds to wait for a key press. If the user presses a key within the specified time, the function returns the key code. Otherwise, it returns -1. In this case, we pass 0 to wait indefinitely for a key press.
Finally, we use the cv2.destroyAllWindows() function to close all the windows. This is optional, but it is good practice to close all the windows before exiting the Python interpreterprogram.
Detail
Input python to start the process of python interpreter. Then input the following code.
window_name = 'Image'
## Display the image, image has been loaded in the previous steps
cv2.imshow(window_name, image)
## Wait for a key press and close the window
cv2.waitKey(0)
## destroy the window after you showing the image
cv2.destroyAllWindows()
NOTE
- You can change the
window_namefor your code and see the result. - You can change the parameter of
waitKeyfor your code and see the result. - For your convenience, we removed the correctness detection of image loading in this step.
- Note that you are using a Python interpreter, so the program does not end immediately after
imshow, but if you are executing a Python file, you must usewaitKeyto ensure that the window does not close afterimshow.
Writing an image to a file
To save an image in a different format, we use the cv2.imwrite() function. The function takes two arguments: the file path and the image to save.
Detail
Input python to start the process of python interpreter. Then input the following code.
## Save the image in a different format, image has been loaded in the previous steps
cv2.imwrite('/home/labex/Desktop/Image.png', image)
Note
- In this case, we save the image in the PNG format (Image.png). The image is saved in the same directory (/home/labex/Desktop/) as the Python script.
- For your convenience, we removed the correctness detection of image loading in this step.
Summary
Now you know the basics of working with images using the OpenCV-Python library. You can experiment with different image formats and explore more advanced operations such as resizing, cropping, and filtering. Happy coding!



