[파이썬] imageio 여러 이미지 동시에 읽기

ImageIO is a powerful Python library that allows for efficient processing and manipulation of images. One common task is to read multiple images simultaneously for batch processing or analysis. In this article, we will explore how to use ImageIO to read multiple images at once in Python.

Installation

Before we begin, make sure you have ImageIO installed on your system. If not, you can install it using pip:

pip install imageio

Reading Multiple Images

To read multiple images simultaneously, we can use the imread_multi function provided by ImageIO. This function takes a list of image file paths as input and returns a NumPy array, where each image is stacked along a new dimension.

Here’s an example:

import imageio

# List of image file paths
image_files = ['/path/to/image1.jpg', '/path/to/image2.jpg', '/path/to/image3.jpg']

# Read multiple images
images = imageio.imread_multi(image_files)

# Access individual images
image1 = images[0]
image2 = images[1]
image3 = images[2]

# Print shape of each image
print("Shape of Image 1:", image1.shape)
print("Shape of Image 2:", image2.shape)
print("Shape of Image 3:", image3.shape)

In this example, we provide a list of image file paths to the imread_multi function, which returns a NumPy array containing all the images. We can then access each individual image from the array.

Batch Processing

Reading multiple images simultaneously can be useful when performing batch processing or analysis on a large number of images. For example, if you want to apply the same image transformation to each image in a directory, you can use a loop to read and process each image one by one. However, using ImageIO’s imread_multi function can significantly speed up the process by reading all the images at once.

Here’s an example of how to read and process multiple images in a directory:

import os
import imageio

# Directory containing images
image_dir = '/path/to/images'

# Get all image files in the directory
image_files = [os.path.join(image_dir, file) for file in os.listdir(image_dir) if file.endswith('.jpg')]

# Read multiple images
images = imageio.imread_multi(image_files)

# Process each image
for i in range(len(images)):
    # Perform image processing operations
    processed_image = process_image(images[i])
    
    # Save processed image
    output_path = os.path.join(image_dir, f"processed_image_{i}.jpg")
    imageio.imwrite(output_path, processed_image)

In this example, we use the os module to get all image files in a directory that have the ‘.jpg’ extension. We then read all the images using imread_multi and process each image individually in a loop. The processed images are saved with a new filename in the same directory.

Conclusion

ImageIO’s ability to read multiple images simultaneously can greatly improve the efficiency of image processing and analysis tasks in Python. By using the imread_multi function, you can quickly read and process large numbers of images in a single operation. This can be particularly beneficial when working with batch processing or analyzing image datasets. Give it a try in your next image processing project!