What it is
scikit-image is a Python library for image processing that provides a collection of algorithms for segmentation, geometric transformations, color space manipulation, filtering, morphology, feature detection, and more.
scikit-image provides functions for reading and writing images, performing geometric and color transformations, applying filters, detecting features, and extracting image statistics. It works seamlessly with NumPy arrays for numerical computations and can be combined with Matplotlib for visualization.
Installation
pip install scikit-imageGetting started
The smallest useful thing you can do with it, and what each part means.
from skimage import io
img = io.imread('image.jpg')
io.imshow(img)
io.show()from skimage.color import rgb2gray
gray_img = rgb2gray(img)
io.imshow(gray_img)
io.show()Advanced usage
Where the library earns its place over a simpler alternative.
from skimage import feature
edges = feature.canny(gray_img)
io.imshow(edges)
io.show()from skimage.transform import resize
resized_img = resize(img, (200, 200))
io.imshow(resized_img)
io.show()from skimage.filters import gaussian
smoothed_img = gaussian(gray_img, sigma=1)
io.imshow(smoothed_img)
io.show()from skimage.measure import label
labeled_img = label(gray_img > 0.5)
io.imshow(labeled_img)
io.show()Errors and fixes
The failures you are most likely to hit, and what actually resolves them.
- ValueError: image dtype not supported
- Convert images to supported dtypes (e.g., float or uint8) using `img_as_float` or `img_as_ubyte`.
- IndexError: tuple index out of range
- Check the image shape and ensure operations are applied to valid dimensions.
- ImportError: No module named 'skimage'
- Install scikit-image using pip or conda in your current Python environment.
Best practices
- Use NumPy arrays as the primary image representation for efficiency.
- Normalize images to float in [0, 1] when using scikit-image filters.
- Leverage built-in visualization functions or Matplotlib for displaying results.
- Use modular functions to chain processing steps cleanly.
- Handle images with varying shapes and channels appropriately.
Background
Why it exists, and what it was reacting to.
scikit-image was developed as part of the scikit-learn ecosystem to provide easy-to-use image processing tools in Python. It integrates closely with NumPy arrays and scientific Python libraries, making it popular for academic research, prototyping, and real-world image processing applications.
