Imageio
Imageio is a mature Python library (current version 2.37.3) that makes it easy to read and write image and video data. This includes animated images, video, volumetric data, and scientific formats. It is cross-platform, runs on Python 3.10+, and is easy to install. The library is actively maintained with a focus on ease of use and broad format support.
Warnings
- breaking The core API of Imageio underwent a significant overhaul with the introduction of `v3`. The older API, previously accessible directly under the `imageio` namespace (e.g., `imageio.imread`), is now considered `imageio.v2` and is deprecated.
- gotcha For reading and writing video files (e.g., MP4, AVI), Imageio requires an external plugin like `imageio-ffmpeg` or `pyav`. Without one of these optional dependencies, video operations will fail (e.g., `iio.imread('video.mp4')` will raise a `PluginNotFoundError`).
- gotcha While Pillow is a required dependency for common image formats, users occasionally encounter issues where Imageio's Pillow plugin cannot be found or loaded, leading to errors when processing common image types (JPEG, PNG). This often stems from environment misconfigurations.
Install
-
pip install imageio
Imports
- iio
import imageio.v3 as iio
Quickstart
import imageio.v3 as iio
import numpy as np
# Read a standard image from imageio's sample data
im = iio.imread('imageio:chelsea.png')
print(f"Read image with shape: {im.shape}, dtype: {im.dtype}")
# Modify the image (e.g., convert to grayscale)
grayscale_im = np.dot(im[...,:3], [0.2989, 0.5870, 0.1140]).astype(np.uint8)
print(f"Grayscale image shape: {grayscale_im.shape}, dtype: {grayscale_im.dtype}")
# Write the modified image to a file
iio.imwrite('chelsea_grayscale.png', grayscale_im)
print("Saved 'chelsea_grayscale.png'")
# Clean up the created file (optional)
import os
# os.remove('chelsea_grayscale.png')