The easiest way to use OpenCV with Python

This tutorial will discuss the easiest way to use OpenCV with Python. As you know, OpenCV (Open Source Computer Vision Library) is an open-source library of programming functions mainly aimed at computer vision tasks. It provides image and video processing tools, object detection, face recognition, and machine learning. Widely used in robotics, AI, and augmented reality, it supports multiple languages like C++, Python, and Java.

Let’s start! First of all, you need to download and install Python.

Next, we will use UV as a package manager. UV is a fast, modern Python package manager developed by Astral. Designed as a drop-in replacement for pip and virtualenv, it uses the Rust programming language for performance, resolving dependencies quickly and managing virtual environments efficiently. UV improves installation speed, reliability, and reproducibility, aiming to streamline Python development workflows with minimal configuration. The UV is 10 to 100 times faster than the pip package manager.

Next, we need to create a virtual environment using uv. Run the following command in the terminal. Here I’m using Python 3.11.6 for the virtual environment, you can use any version you wish to work with. The virtual environment will be created in env folder.

uv venv --python 3.11.6 env

To activate the virtual environment, run the following command :

For Windows users :

env\Scripts\activate

Linux\Mac users :

source venv/bin/activate

Finally, we can install opencv using uv. Run the following command to install it :

uv pip install opencv-python

Or you can install opencv-headless using the below command

uv pip install opencv-python-headless

The difference between opencv-python and opencv-python-headless lies mainly in GUI support and dependencies:

OpenCV-Python

  • Includes GUI functionality (e.g., cv2.imshow(), cv2.waitKey())
  • Requires GUI-related system libraries like GTK or Qt.
  • Suitable for local development or environments with a display.

Opencv-python-headless

  • Excludes GUI features, so no imshow() window handling.
  • Smaller in size and fewer dependencies.
  • Ideal for server-side, Docker, CI/CD, or headless environments.

After installation, you can run the following OpenCV test code :

import cv2
import numpy as np

print("OpenCV version:", cv2.__version__)

image = cv2.imread("test.jpg")
if image is None:
    print("Image not found,creating a dummy image")
    image = 255 * np.ones((200, 200, 3), dtype=np.uint8)

# Display the image (only works with opencv-python, not headless)
cv2.imshow("Test Image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()

The output of the app :

OpenCV Python demo

Conclusion

In conclusion, it is easy to install and and use OpenCV with Python using uv package manager.