Getting Started with Images

OpenCVOpenCVBeginner
Practice Now

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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL opencv(("`OpenCV`")) -.-> opencv/BasicOperationsGroup(["`Basic Operations`"]) opencv/BasicOperationsGroup -.-> opencv/imread("`Image Read`") opencv/BasicOperationsGroup -.-> opencv/wait_key("`Wait Key`") opencv/BasicOperationsGroup -.-> opencv/destroy_all_windows("`Destroy Windows`") opencv/BasicOperationsGroup -.-> opencv/imshow("`Image Show`") opencv/BasicOperationsGroup -.-> opencv/imwrite("`Image Write`") subgraph Lab Skills opencv/imread -.-> lab-8438{{"`Getting Started with Images`"}} opencv/wait_key -.-> lab-8438{{"`Getting Started with Images`"}} opencv/destroy_all_windows -.-> lab-8438{{"`Getting Started with Images`"}} opencv/imshow -.-> lab-8438{{"`Getting Started with Images`"}} opencv/imwrite -.-> lab-8438{{"`Getting Started with Images`"}} end

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

  1. You can change the window_name for your code and see the result.
  2. You can change the parameter of waitKey for your code and see the result.
  3. For your convenience, we removed the correctness detection of image loading in this step.
  4. 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 use waitKey to ensure that the window does not close after imshow.

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

  1. 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.
  2. 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!

Other OpenCV Tutorials you may like