[파이썬] imageio 이미지 일괄 처리

ImageIO Python Library

Processing images in bulk is a common task in various domains such as computer vision, deep learning, and data preprocessing. Python provides several libraries for image processing, and one of the popular choices is ImageIO.

ImageIO is a Python library that allows us to read, write, and manipulate a wide range of image file formats. It provides a simple and intuitive API for performing various operations on images, such as loading images from files, saving images, resizing, cropping, and applying different filters.

In this blog post, we will explore the basics of using ImageIO for batch processing of images in Python.

Installation

Before we dive into the code examples, we need to install ImageIO. You can easily install it using pip:

pip install imageio

Make sure you have Python and pip installed on your system before running the command.

Loading and Saving Images

The first step in image processing is often loading images from files and saving the processed output. Here’s how you can use ImageIO to achieve this:

import imageio

# Loading an image from file
image = imageio.imread("input.jpg")

# Saving the image
imageio.imwrite("output.jpg", image)

In the above code snippet, we use the imageio.imread function to read an image from a file (in this case, “input.jpg”). The resulting image is represented as a NumPy array, which allows us to easily manipulate the image data.

Next, we use the imageio.imwrite function to save the image to a file (in this case, “output.jpg”). You can choose the output format by providing the appropriate file extension (e.g., “.jpg”, “.png”, etc.).

Batch Processing Images

Now, let’s move on to batch processing multiple images. To illustrate this, let’s say we have a folder containing multiple image files, and we want to resize all of them to a specific size.

import os
import imageio

# Path to the folder containing input images
input_folder = "input_images"

# Path to the folder to save the processed images
output_folder = "output_images"

# Get a list of all image file names in the input folder
image_files = os.listdir(input_folder)

# Iterate over each image file
for image_file in image_files:
    # Construct the full file paths
    input_path = os.path.join(input_folder, image_file)
    output_path = os.path.join(output_folder, image_file)

    # Load the image
    image = imageio.imread(input_path)

    # Resize the image to a specific size
    resized_image = imageio.imresize(image, (256, 256))
    
    # Save the resized image
    imageio.imwrite(output_path, resized_image)

In the above code snippet, we first specify the input folder (where the original images are located) and the output folder (where the processed images will be saved).

We then use os.listdir to get a list of all the image file names in the input folder. This allows us to iterate over each image file using a for loop.

Inside the loop, we construct the full file paths using os.path.join for both the input and output images. We load the image from the input path using imageio.imread and resize it to the desired size (256x256) using imageio.imresize.

Finally, we save the resized image to the output path using imageio.imwrite.

Conclusion

ImageIO is a powerful Python library for batch processing images, providing a simple and efficient way to handle common image processing tasks. It allows us to easily load images from files, save processed images, and perform various transformations on the images.

In this blog post, we covered the basics of using ImageIO for batch processing, including loading and saving images, as well as resizing images. Armed with this knowledge, you can now explore the capabilities of ImageIO and apply it to your own image processing projects.

Remember to explore the official ImageIO documentation for more detailed information and additional functionalities.

Happy coding!