[파이썬] imageio 이미지 리사이징

In today’s digital world, working with images is an essential part of many applications. Whether you are building a website, a computer vision system, or simply doing image processing, the ability to resize images is a common requirement. In this blog post, we will explore how to use imageio, a popular image processing library in Python, to resize images.

Installation

Before we dive into the code, let’s make sure we have imageio installed in our Python environment. You can install it using pip by running the following command:

pip install imageio

Resizing Images

To resize an image using imageio, we first need to import the necessary modules:

import imageio
import skimage.transform as transform

Next, we need to read the image that we want to resize:

image = imageio.imread('path/to/image.jpg')

Now that we have the image loaded, we can proceed with resizing it. The transform.resize function from the scikit-image module allows us to easily resize the image to our desired dimensions:

resized_image = transform.resize(image, (new_width, new_height))

Here, new_width and new_height represent the dimensions we want the new image to have. The resize function will automatically handle resizing the image while maintaining its aspect ratio.

Saving the Resized Image

After resizing the image, we might want to save it to disk. We can do this using the imageio.imwrite function:

imageio.imwrite('path/to/resized_image.jpg', resized_image)

This will save the resized image as a new file with the provided path.

Example Usage

Let’s put everything together and resize an image using imageio:

import imageio
import skimage.transform as transform

# Read the image
image = imageio.imread('path/to/image.jpg')

# Resize the image
new_width = 800
new_height = 600
resized_image = transform.resize(image, (new_width, new_height))

# Save the resized image
imageio.imwrite('path/to/resized_image.jpg', resized_image)

In this example, we read an image, resize it to a width of 800 pixels and a height of 600 pixels, and save the resized image to the specified path.

Conclusion

Resizing images is a common task in image processing, and imageio provides a simple and efficient way to accomplish this in Python. With the ability to read, resize, and save images, you can now incorporate image resizing into your Python applications easily. So go ahead and give it a try!