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.
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.
First, let's import the required libraries. You will use the cv2
library for image processing. Please use import
for importing it.
Open the Python shell by typing the following command in the terminal at the VM Desktop.
python3
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.
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
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.')
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.
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 interpreter
program.
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()
window_name
for your code and see the result.waitKey
for your code and see the result.imshow
, but if you are executing a Python file, you must use waitKey
to ensure that the window does not close after imshow
.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.
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)
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!