Common Python Libraries for Image Processing: Overview and Code Examples
This article introduces the most widely used Python image‑processing libraries—including scikit‑image, NumPy, SciPy, Pillow, OpenCV‑Python, SimpleCV, Mahotas, SimpleITK, pgmagick, and Pycairo—explaining their key features and providing concise code snippets that demonstrate filtering, segmentation, enhancement, and computer‑vision tasks.
Image processing is essential for analyzing and manipulating digital images, and Python offers a rich ecosystem of libraries to perform common tasks such as filtering, segmentation, feature extraction, and visualisation.
scikit‑image is an open‑source package built on NumPy arrays that provides research‑grade algorithms for education and industry. Example usage:
<code>import matplotlib.pyplot as plt
%matplotlib inline
from skimage import data, filters
image = data.coins()
# ... or any other NumPy array!
edges = filters.sobel(image)
plt.imshow(edges, cmap='gray')</code>NumPy is the core array library for Python; images are essentially NumPy arrays, so basic slicing, masking and fancy indexing can modify pixel values. Example:
<code>import numpy as np
from skimage import data
import matplotlib.pyplot as plt
%matplotlib inline
image = data.camera()
mask = image < 87
image[mask] = 255
plt.imshow(image, cmap='gray')</code>SciPy extends NumPy with scientific utilities; its scipy.ndimage submodule offers n‑dimensional filtering, morphology, spline interpolation and measurement functions. Example of Gaussian blur:
<code>from scipy import misc, ndimage
face = misc.face()
blurred_face = ndimage.gaussian_filter(face, sigma=3)
very_blurred = ndimage.gaussian_filter(face, sigma=5)
plt.imshow(blurred_face)</code>Pillow (PIL fork) adds support for opening, processing and saving many image formats. It provides point operations, convolution kernels and colour‑space conversion. Example of contrast enhancement:
<code>from PIL import Image, ImageFilter
im = Image.open('image.jpg')
im.show()
from PIL import ImageEnhance
enh = ImageEnhance.Contrast(im)
enh.enhance(1.8).show('30% more contrast')</code>OpenCV‑Python is the Python API for the widely used OpenCV computer‑vision library, offering fast C/C++‑backed functions with a Pythonic front‑end. It excels at computationally intensive vision pipelines.
SimpleCV provides a higher‑level wrapper around OpenCV and other vision libraries, simplifying tasks for beginners while still exposing powerful features such as camera access, video files and image streams.
Mahotas is a C++‑backed library offering traditional image‑processing functions and modern computer‑vision utilities (e.g., interest‑point detection). It is lightweight with minimal dependencies.
SimpleITK builds on the Insight Segmentation and Registration Toolkit (ITK) to provide a simplified, cross‑language interface for image analysis, supporting filtering, segmentation and registration in Python notebooks.
pgmagick is a Python wrapper for the GraphicsMagick library, a Swiss‑army‑knife‑style image‑processing system supporting over 88 formats and offering powerful read/write/manipulation capabilities.
Pycairo provides Python bindings for the Cairo 2D vector graphics library, enabling resolution‑independent drawing of lines, shapes and radial gradients directly from Python.
These libraries together cover a broad spectrum of image‑processing needs, from simple array manipulation to advanced computer‑vision pipelines, and are freely available for experimentation and production use.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.