[파이썬] Pillow SD 파일 다루기

Pillow is a popular Python library for image processing. It provides a wide range of functions for handling and manipulating images. In this blog post, we will focus on how to work with SD (Secure Digital) files using Pillow in Python.

What is an SD file?

An SD file is a storage format commonly used for storing digital images. It is the standard memory card format used in various devices such as cameras, smartphones, and tablets. These files typically have extensions like .jpg, .jpeg, .png, etc.

Installing Pillow

Before we can start working with SD files in Python, we need to install Pillow. You can install it using pip by running the following command in your terminal:

pip install pillow

Once Pillow is installed, we can begin using its powerful image manipulation capabilities.

Reading an SD file

To read an SD file using Pillow, we can use the Image.open() function. Here’s an example:

from PIL import Image

# Open an SD file
image = Image.open("example.jpg")

# Display some information about the image
print("Image format:", image.format)
print("Image size:", image.size)
print("Image mode:", image.mode)

# Show the image
image.show()

In the above code snippet, we import the Image module from Pillow, open an SD file called “example.jpg”, and display some basic information about the image such as its format, size, and mode. Finally, we display the image using the show() method.

Modifying an SD file

Pillow provides many functions to modify SD files. Here are some commonly used functions:

Resizing an image

from PIL import Image

# Load the image
image = Image.open("example.jpg")

# Resize the image
resized_image = image.resize((500, 500))

# Save the resized image
resized_image.save("resized_example.jpg")

In the above code, we open the image using Image.open(), resize it to 500x500 pixels using the resize() function, and save the resized image using the save() method.

Rotating an image

from PIL import Image

# Load the image
image = Image.open("example.jpg")

# Rotate the image by 90 degrees clockwise
rotated_image = image.rotate(-90)

# Save the rotated image
rotated_image.save("rotated_example.jpg")

In this example, we rotate the image by 90 degrees clockwise using the rotate() function and save the rotated image.

Conclusion

In this blog post, we learned how to work with SD files using Pillow in Python. We saw how to read an SD file, display information about the image, and perform common image modifications such as resizing and rotating.

Pillow provides many more functions for manipulating images, so feel free to explore the documentation for more options and capabilities. Happy image processing!