Pillow is a popular Python library for working with images. It provides a wide range of image processing capabilities, including opening, manipulating, and saving various image file formats. Pillow is an open-source library and is the successor to the Python Imaging Library (PIL).
Here are some common tasks you can perform using Pillow:
- Opening an image:
from PIL import Image
image = Image.open("example.jpg")
- Displaying an image:
image.show()
- Resizing an image:
new_size = (width, height)
resized_image = image.resize(new_size)
- Rotating an image:
rotated_image = image.rotate(90)
- Saving an image:
image.save("output.jpg")
- Converting between different image modes (e.g., RGB to grayscale):
grayscale_image = image.convert("L")
- Cropping an image:
box = (left, upper, right, lower)
cropped_image = image.crop(box)
- Adding text to an image:
from PIL import ImageDraw, ImageFont
draw = ImageDraw.Draw(image)
font = ImageFont.truetype("arial.ttf", size=36)
draw.text((x, y), "Hello, Pillow!", fill="red", font=font)
Pillow provides extensive documentation and is widely used in various image processing applications in Python. You can install it using pip:
pip install Pillow
Make sure to refer to the Pillow documentation for more details and examples: https://pillow.readthedocs.io/en/stable/index.html