OpenCV (Open Source Computer Vision Library) is a highly optimized open-source library for computer vision and machine learning tasks. The `opencv-python` package provides official Python bindings, enabling developers to access its extensive functionalities for image and video processing, object detection, and more within Python applications. It is actively maintained with frequent minor releases, often on a monthly cadence, to incorporate new features, bug fixes, and support for the latest Python and NumPy versions.
pip install opencv-pythonVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to load, display, and then close an image using `opencv-python`. It includes error handling for failed image loading and ensures proper window closure. A dummy image is created if 'test_image.jpg' does not exist.
Upgrade `opencv-python` to version 4.10.0.84 or later (or the latest available version that explicitly supports NumPy 2.x for your Python version). Alternatively, downgrade NumPy to a version less than 2.0 (`pip install "numpy<2.0"`) if upgrading OpenCV is not an option.
For headless environments, use `pip install opencv-python-headless` instead. This package provides core OpenCV functionality without the GUI components.
Always check if the returned object (e.g., `img` from `cv2.imread()`, `success` from `cap.read()`) is valid (not `None` or `False`) before proceeding with image/frame processing.
Choose the correct package for your needs: `opencv-python` for core modules with GUI, `opencv-contrib-python` for core + extra modules with GUI, `opencv-python-headless` for core modules without GUI, and `opencv-contrib-python-headless` for core + extra modules without GUI. Do not install multiple variants in the same environment.
Convert the image from BGR to RGB using `cv2.cvtColor(img, cv2.COLOR_BGR2RGB)` before displaying it with RGB-expecting libraries.
Ensure the necessary build tools and C/C++ compilers are installed on the system *before* attempting `pip install opencv-python`. For `alpine` Linux, this typically involves `apk add python3-dev build-base cmake ninja`. Alternatively, use a base image for which pre-built `opencv-python` wheels are available (e.g., Debian/Ubuntu), or consider using `opencv-python-headless` which might have fewer build dependencies.
Ensure you install the correct package in your active Python environment: `pip install opencv-python` or `pip install opencv-contrib-python` for extra modules.
Verify Python and `opencv-python` version compatibility, upgrade `pip` (`python -m pip install --upgrade pip`), consider installing `opencv-python-headless` if GUI components are not needed, or ensure the necessary Visual C++ Redistributables are installed and updated. Sometimes, a complete uninstall and reinstall of `opencv-python` resolves the issue.
First, check if you have a file named `cv2.py` in your project directory and rename it. If the error persists for basic functions, uninstall `opencv-python` and reinstall it. If it's for an advanced module, install `opencv-contrib-python`: `pip install opencv-contrib-python`.
Use the correct package name for installation: `pip install opencv-python` or `pip install opencv-contrib-python` if you need extra modules.