OpenCV Python Headless

4.13.0.92 · active · verified Sat Mar 28

opencv-python-headless is a wrapper package for OpenCV (Open Source Computer Vision Library) Python bindings, specifically compiled without graphical user interface (GUI) support. This makes it ideal for server-side processing, cloud deployments, Docker containers, and other headless environments where displaying images or videos directly is not required. It provides core computer vision functionalities and is currently at version 4.13.0.92, with regular releases aligning with OpenCV's main development cycle.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic image processing in a headless environment. It creates a dummy image, reads it using `cv2.imread()`, converts it to grayscale, applies a Gaussian blur, and then saves the result using `cv2.imwrite()`. This sequence avoids GUI-dependent functions like `cv2.imshow()`.

import cv2
import numpy as np
import os

# Create a dummy image (e.g., 100x100 white image)
dummy_image_path = 'dummy_image.png'
img = np.zeros((100, 100, 3), dtype=np.uint8)
img.fill(255) # White image

# Save the dummy image
cv2.imwrite(dummy_image_path, img)

# Read the image
image = cv2.imread(dummy_image_path)

if image is not None:
    print(f"Image loaded successfully with shape: {image.shape}")
    
    # Convert to grayscale
    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    
    # Apply a simple blur
    blurred_image = cv2.GaussianBlur(gray_image, (5, 5), 0)
    
    # Save the processed image (e.g., 'processed_image.png')
    output_path = 'processed_image.png'
    cv2.imwrite(output_path, blurred_image)
    print(f"Processed image saved to {output_path}")
    
    # Clean up dummy image
    os.remove(dummy_image_path)
    os.remove(output_path)
else:
    print("Error: Could not load image.")

view raw JSON →