Installation
This page summarizes common installation options for OpenCV in Python:
Installation with pip
Section titled “Installation with pip”The easiest way to install OpenCV is using pip:
pip install opencv-pythonTo install the version with additional contributions:
pip install opencv-contrib-pythonInstallation with Conda
Section titled “Installation with Conda”If you use Conda or Miniconda:
conda install -c conda-forge opencvOr create a specific Conda environment:
conda create -n opencv-env python=3.10conda activate opencv-envconda install -c conda-forge opencvPlatform-specific notes
Section titled “Platform-specific notes”# Make sure you have Homebrew installedbrew install python3
# Then install OpenCVpip3 install opencv-python# Install system dependenciessudo apt-get updatesudo apt-get install python3-pip python3-dev
# Install OpenCVpip3 install opencv-python# Use PowerShell or CMDpython -m pip install opencv-pythonVerify the installation
Section titled “Verify the installation”-
Create a verification script:
Python import cv2import systry:# Check versionversion = cv2.__version__print(f"✓ OpenCV {version} installed correctly")# Verify we can read an imageimg = cv2.imread("test.jpg") if len(sys.argv) > 1 else Noneif img is not None:print(f"✓ Image reading working")print(f" Dimensions: {img.shape}")else:print("ℹ Create a 'test.jpg' image to test reading")# Verify basic operationstest_img = cv2.imread("test.jpg") if len(sys.argv) > 1 else Noneif test_img is not None:gray = cv2.cvtColor(test_img, cv2.COLOR_BGR2GRAY)print("✓ Color conversion working")except ImportError as e:print(f"✗ Error: {e}")print(" Make sure you've installed opencv-python")except Exception as e:print(f"⚠ Warning: {e}")print("\nInstallation verified!") -
Run the script:
Terminal window python verify_opencv.py -
If this code runs without errors and prints a version, your installation is working.