Imageio is a user-friendly Python library that simplifies the process of reading and writing various image data types. It supports a wide range of formats (like PNG and JPEG), animated images (GIF), volumetric data (such as medical imaging), and scientific data formats. With Imageio, you can easily handle tasks such as image manipulation, conversion, and analysis. It is cross-platform, runs on Python 3.9+, and is easy to install. Imageio's flexibility and ease of use make it a popular choice for both beginners and advanced users in the field of image processing.
Minimal requirements:
--> Python 3.9+
--> NumPy
--> Pillow >= 8.3.2
To install imageio, use one of the following methods:
--> If you are in a conda env:
conda install -c conda-forge imageio
pip install imageio
python setup.py install
import imageio
1. Reading image:-
import imageio.v3 as iio
#Reading image file "image.png"
im = iio.imread('image.png')
print(im.shape)
# for animated image:
import imageio.v3 as iio
# index=None means: read all images in the file and stack along first axis
frames = iio.imread("anime.gif", index=None)
# ndarray with (num_frames, height, width, channel)
print(frames.shape)
Output:-
2. Reading from external sources:-
import imageio.v3 as iio
import io
# from HTTPS
web_image = "https://upload.wikimedia.org/wikipedia/commons/d/d3/Newtons_cradle_animation_book_2.gif"
frames = iio.imread(web_image, index=None)
# from bytes
bytes_image = iio.imwrite("<bytes>", frames, extension=".gif")
frames = iio.imread(bytes_image, index=None)
# from byte streams
byte_stream = io.BytesIO(bytes_image)
frames = iio.imread(byte_stream, index=None)
# from file objects
class MyFileObject:
def read(size:int=-1):
return bytes_image
def close():
return # nothing to do
frames = iio.imread(MyFileObject())
3. Read all Images in a Folder :-
import imageio.v3 as iio
from pathlib import Path
images = list()
for file in Path("path/to/folder").iterdir():
if not file.is_file():
continue
images.append(iio.imread(file))
4.Grab screenshot or image from the clipboard
import imageio.v3 as iio
im_screen = iio.imread('<screen>')
im_clipboard = iio.imread('<clipboard>')
5.Grab frames from your webcam
--> For this to work, you need to install the ffmpeg backend:
pip install imageio[ffmpeg]
import imageio.v3 as iio
import numpy as np
for idx, frame in enumerate(iio.imiter("<video0>")):
print(f"Frame {idx}: avg. color {np.sum(frame, axis=-1)}")
Output:-
6. Convert a short movie to grayscale
--> For this to work, you need to install the ffmpeg backend:
pip install imageio[ffmpeg]
import imageio as iio
import numpy as np
# Read the video using get_reader
video_reader = iio.get_reader("example.mp4")
# Extract metadata
metadata = video_reader.get_meta_data()
# Read frames
frames = []
for frame in video_reader:
frames.append(frame)
frames = np.array(frames)
# Manually convert the video to grayscale
gray_frames = np.dot(frames[..., :3], [0.2989, 0.5870, 0.1140])
gray_frames = np.round(gray_frames).astype(np.uint8)
gray_frames_as_rgb = np.stack([gray_frames] * 3, axis=-1)
# Write the video
iio.mimwrite("example_gray.mp4", gray_frames_as_rgb, fps=metadata["fps"])
--> example.mp4
Output:-
--> example_grey.mp4
7. Read or iterate frames in a video
--> For this to work, you need to install the pyav backend:
pip install av
import imageio.v3 as iio
# read a single frame
frame = iio.imread(
"example.mp4",
index=42,
plugin="pyav",
)
# bulk read all frames
# Warning: large videos will consume a lot of memory (RAM)
frames = iio.imread("example.mp4", plugin="pyav")
# iterate over large videos
for frame in iio.imiter("example.mp4", plugin="pyav"):
print(frame.shape, frame.dtype)
Output:-
.................
(270, 480, 3) uint8
8. Re-encode a (large) video
--> For this to work, you need to install the pyav backend:
pip install av
import imageio.v3 as iio
# assume this is too large to keep all frames in memory
source = "example.mp4"
dest = "reencoded_example.mkv"
fps = iio.immeta(source, plugin="pyav")["fps"]
with iio.imopen(dest, "w", plugin="pyav") as out_file:
out_file.init_video_stream("vp9", fps=fps)
for frame in iio.imiter(source, plugin="pyav"):
out_file.write_frame(frame)
Output:-
--> reencoded_example.mkv
9. Read medical data (DICOM)
import imageio.v3 as iio
dirname = 'path/to/dicom/files'
# Read multiple images of different shape
ims = [img for img in iio.imiter(dirname, plugin='DICOM')]
# Read as volume
vol = iio.imread(dirname, plugin='DICOM')
# Read multiple volumes of different shape
vols = [img for img in iio.imiter(dirname, plugin='DICOM')]
10. Writing videos with FFMPEG and vaapi
Using vaapi can help free up CPU time on your device while you are encoding videos. One notable difference between vaapi and x264 is that vaapi doesn’t support the color format yuv420p.
--> you will need ffmpeg compiled with vaapi for this to work.
import imageio.v2 as iio
import numpy as np
# All images must be of the same size
image1 = np.stack([iio.imread('mountain.jpg')] * 3, 2)
image2 = iio.imread('sky1.jpg')
image3 = iio.imread('sky2.jpg')
# Write the video
w = iio.get_writer('my_video.mp4', format='FFMPEG', mode='I', fps=1, codec='libx264')
w.append_data(image1)
w.append_data(image2)
w.append_data(image3)
w.close()
A little bit of explanation:
---> output_params
* vaapi_device specifies the encoding device that will be used.
* vf and format tell ffmpeg that it must upload to the dedicated hardware. Since vaapi only supports a subset of color formats, we ensure that the video is in either gray or nv12 before uploading it. The or operation is achieved with |.
---> pixelformat: set to 'vaapi_vld' to avoid a warning in ffmpeg.
---> codec: the code you wish to use to encode the video. Make sure your hardware supports the chosen codec. If your hardware supports h265, you may be able to encode using 'hevc_vaapi'
-->mountain.jpg
-->sky1.jpg
-->sky2.jpg
--> Output:-
Data Analysis: Incorporate imageio into your data analysis workflow to easily read, manipulate, and save image data. It's perfect for visualising data insights and generating graphical reports. It is also used in machine learning to preprocess images for machine learning models by resizing, cropping, and normalising them with imageio. It's beneficial for preparing large datasets for training and testing image recognition algorithms.
Medical Imaging: Use imageio to handle medical images such as X-rays, MRIs, and CT scans. Its support for different image formats ensures compatibility with various medical imaging software and systems.
Web Development: Integrate imageio into web applications to handle image uploads, processing, and storage. It helps create dynamic web content and improve user experience.
Happy coding!
Moram Raghavendra Sri Koushik ( 24110210 )
Bhava Ram ( 24110083 )
Saladi Suchith ( 24110313 )
IITGn Students B-Tech 1st year
For PSDV Course Project
Website Building Software