Common Python Image‑Processing Libraries and Usage Examples
This article introduces the most popular Python libraries for image processing—including scikit‑image, NumPy, SciPy, Pillow, OpenCV, SimpleCV, Mahotas, SimpleITK, pgmagick, and Pycairo—explains their core features, and provides concise code snippets demonstrating tasks such as filtering, template matching, masking, blurring, and visualisation.
The article begins by explaining the importance of image processing and why Python is a preferred language due to its rich ecosystem of free, open‑source libraries.
01 scikit‑Image – an open‑source package built on NumPy arrays offering research‑grade algorithms. Example usage shows loading an image, applying a Sobel filter, and displaying the result:
<code>import matplotlib.pyplot as plt
%matplotlib inline
from skimage import data, filters
image = data.coins()
edges = filters.sobel(image)
plt.imshow(edges, cmap='gray')</code>02 NumPy – the core array library; images are essentially NumPy arrays, enabling slicing, masking, and fancy indexing. Example code demonstrates creating a mask and modifying pixel values.
<code>import numpy as np
from skimage import data
image = data.camera()
mask = image < 87
image[mask] = 255
plt.imshow(image, cmap='gray')</code>03 SciPy – provides scientific tools, including the ndimage submodule for multi‑dimensional filtering. A Gaussian blur example is shown:
<code>from scipy import misc, ndimage
face = misc.face()
blurred_face = ndimage.gaussian_filter(face, sigma=3)
plt.imshow(blurred_face)</code>04 Pillow (PIL) – a maintained fork of the original Python Imaging Library, supporting many image formats and basic operations such as filtering and colour‑space conversion. Example enhances contrast using ImageEnhance :
<code>from PIL import Image, ImageFilter, ImageEnhance
im = Image.open('image.jpg')
enh = ImageEnhance.Contrast(im)
enh.enhance(1.8).show()</code>05 OpenCV‑Python – the Python API for the widely used OpenCV computer‑vision library, offering fast C/C++ back‑ends with a Python front‑end. An example of creating image pyramids is described (illustrated with an image).
06 SimpleCV – a high‑level framework that wraps OpenCV and other vision libraries, simplifying tasks for beginners. It supports cameras, video files, images, and streams.
07 Mahotas – a fast library written in C++ with Python bindings, offering filtering, morphology, and feature extraction. A brief example shows its concise syntax for a “find‑Wally” style task.
08 SimpleITK – a simplified layer over the Insight Segmentation and Registration Toolkit (ITK), providing easy‑to‑use image analysis tools for filtering, segmentation, and registration, with support for Python and R.
09 pgmagick – Python bindings for GraphicsMagick, a versatile image‑processing system supporting over 80 formats, useful for scaling and edge extraction.
10 Pycairo – Python bindings for the cairo 2D vector graphics library, enabling drawing of lines, shapes, and gradients.
The article concludes by encouraging readers to try these free libraries and mentions a QR‑code promotion for a free Python learning resource.
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.