LearnAI ToolsCareerPractice BuildsPlayContact
Lesson 1720 min read

Computer Vision Libraries (OpenCV & Pillow)

Learn OpenCV for image and video processing pipelines and Pillow for everyday image loading, resizing, and format conversion, with working examples of each.

Introduction

Working with images in Python usually means reaching for one of two libraries: OpenCV, a heavyweight computer vision toolkit built for complex processing pipelines and object detection, or Pillow, a lightweight library for everyday tasks like resizing, cropping, and converting image formats.

This lesson covers both, with an edge-detection example in OpenCV and a resize-and-save example in Pillow.

What You Will Learn
  • What OpenCV is and where it is used in computer vision pipelines.
  • How to install opencv-python and run edge detection on an image.
  • What Pillow is and how it differs from OpenCV.
  • How to resize, convert, and save images with Pillow.
  • When to reach for Pillow versus OpenCV.

What is OpenCV?

OpenCV (Open Source Computer Vision Library) is a comprehensive toolkit for image and video processing, originally written in C++ with Python bindings. It covers everything from basic filtering and edge detection to face detection, object tracking, and the preprocessing pipelines that feed into deep learning models for computer vision.

The pip package is named opencv-python, but it is imported in Python as cv2 — a naming holdover from OpenCV's original C++ version 2 API.

pip install opencv-python

Example: Edge Detection with OpenCV

Edge detection highlights the boundaries between regions of an image, and is a common early step in object detection and image analysis pipelines. The example below loads an image, converts it to grayscale, and applies the Canny edge detector.

import cv2
# Load an image from disk
image = cv2.imread("photo.jpg")
# Convert to grayscale, since edge detection does not need color information
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Detect edges using the Canny algorithm
edges = cv2.Canny(gray, threshold1=100, threshold2=200)
# Save the result to a new file
cv2.imwrite("photo_edges.jpg", edges)
print(f"Original shape: {image.shape}")
print(f"Edges shape: {edges.shape}")
Output

Click Run to see what this code prints.

The original image has 3 color channels (blue, green, red — OpenCV loads images in BGR order, not RGB), while the edges output has just one channel, since it is a black-and-white map of where edges were detected.

What is Pillow?

Pillow (imported as PIL, a continuation of the older Python Imaging Library) is a much simpler library focused on everyday image tasks: opening, resizing, cropping, rotating, converting between formats, and basic drawing. It does not include OpenCV's advanced computer vision algorithms, but for straightforward image manipulation it is faster to write and easier to read.

pip install Pillow

Example: Resizing and Converting with Pillow

from PIL import Image
# Open an image
img = Image.open("photo.jpg")
print(f"Original size: {img.size}, format: {img.format}")
# Resize to a thumbnail while preserving aspect ratio
img.thumbnail((300, 300))
# Convert to grayscale and save as PNG
grayscale = img.convert("L")
grayscale.save("photo_thumbnail.png")
print(f"Resized size: {img.size}")
Output

Click Run to see what this code prints.

Pillow vs OpenCV

TaskReach for PillowReach for OpenCV
Resizing, cropping, format conversionYes — simpler APIPossible, but more verbose
Edge/feature detection, filtering algorithmsNot availableYes — this is its core strength
Real-time video processingNot designed for thisYes — built for video streams and webcams
Preparing images for a deep learning modelFine for simple resizing/normalizationCommon in more complex preprocessing pipelines

Common Mistakes

Avoid These Mistakes
  • Forgetting that OpenCV loads images in BGR channel order, not RGB — colors will look wrong if you pass a cv2 image straight into a library expecting RGB.
  • Using img.resize() when you actually want img.thumbnail(), which preserves aspect ratio automatically.
  • Reaching for OpenCV's full API for a simple resize/convert task when Pillow would be far less code.

Best Practices

  • Use Pillow for simple, everyday image tasks — it is lighter weight and easier to read.
  • Use OpenCV when you need real computer vision algorithms, video processing, or performance-critical pipelines.
  • Convert between the two when needed: OpenCV images are just NumPy arrays, so cv2 and Pillow images can be converted back and forth with a channel-order swap.
  • Always check img.size and img.mode (Pillow) or image.shape (OpenCV) after loading, to catch unexpected image formats early.

Frequently Asked Questions

It is a historical naming holdover from OpenCV's C++ version 2 API, which the Python bindings were originally built against. The name stuck even as OpenCV moved well past version 2.

Yes, this is common — for example, using Pillow for simple loading and format handling, then converting to a NumPy array for OpenCV's more advanced processing functions.

Not strictly — frameworks like TensorFlow and PyTorch have their own image loading utilities — but OpenCV is still very commonly used for the preprocessing and postprocessing steps around a deep learning model, like reading video frames or drawing detection boxes.

Key Takeaways

  • OpenCV (opencv-python, imported as cv2) is a full computer vision toolkit for image and video processing.
  • cv2.Canny() is a classic edge detection algorithm, one of many algorithms OpenCV provides.
  • Pillow (imported as PIL) is a lightweight library for everyday image tasks like resizing and format conversion.
  • img.thumbnail() resizes while preserving aspect ratio; img.convert() changes color modes.
  • Use Pillow for simple tasks, OpenCV for real computer vision pipelines and video.

Summary

OpenCV and Pillow cover the two ends of the image-processing spectrum in Python: OpenCV for serious computer vision pipelines, and Pillow for quick, everyday image manipulation. Most projects that touch images end up using at least one of the two, and larger ones often use both.

Lesson 17 Completed
  • You ran edge detection on an image with OpenCV.
  • You resized, converted, and saved an image with Pillow.
  • You are ready to look at the notebook tools data scientists use to explore all of these libraries interactively.
Next Lesson →

Notebook & Interactive Development Tools (Jupyter)