[파이썬] Pillow 이미지 파일 형식 간의 변환

Python provides a powerful library called Pillow, which allows you to work with images. One of the most common tasks when working with images is converting them from one file format to another. In this blog post, we will explore how to use Pillow to convert image files between different formats.

Installing Pillow

Before we get started, make sure you have Pillow installed on your system. You can install Pillow using pip:

pip install pillow

Converting Image Formats

To convert an image file from one format to another using Pillow, you need to follow these steps:

  1. Open the image file using the Image.open() method, specifying the file path as the parameter.
  2. Use the save() method to save the image in the desired format, specifying the output file path and the desired format as parameters.

Here’s an example that demonstrates how to convert an image file from PNG to JPEG:

from PIL import Image

# Open the PNG image file
image = Image.open('input.png')

# Save the image as JPEG
image.save('output.jpeg', 'JPEG')

In this example, we use the Image.open() method to open the input PNG file and assign it to the image variable. Then, we use the save() method to save the image as a JPEG file with the name output.jpeg.

Supported Image Formats

Pillow supports a wide range of image file formats, including PNG, JPEG, GIF, BMP, TIFF, and many others. When saving an image, you can specify the desired format by providing the appropriate file extension in the output file name.

Conclusion

In this blog post, we have learned how to use the Pillow library in Python to convert image files between different formats. Pillow provides a simple and straightforward way to perform this task, making it easy to work with various image file types. Experiment with different formats and enjoy manipulating images in Python effortlessly!