The Pillow library, often referred to as PIL (Python Imaging Library), is a powerful Python library for opening, manipulating, and saving many different image file formats. It provides a wide range of image processing capabilities, making it suitable for tasks such as image enhancement, manipulation, analysis, and generation.
More Information: https://python-pillow.org/
Image.open()
for opening images and Image.save()
for saving images.Image.resize()
), cropping (Image.crop()
), rotating (Image.rotate()
), flipping (Image.transpose()
), and pasting (Image.paste()
).ImageDraw
module. It provides functions for drawing lines, rectangles, ellipses, polygons, and text.Image.new()
), generating gradients, or combining multiple images.Pillow can be installed using Python package managers like pip or conda. It’s available on PyPI (Python Package Index) and can be installed using the following command.
pip install pillow
Pillow has an active community of developers and users who contribute to its development and provide support. There are extensive documentation, tutorials, and online resources available for learning and using Pillow, including the official Pillow documentation and community-contributed tutorials and examples.
Overall, Pillow is a versatile and powerful library for image processing and manipulation in Python. Its rich set of features, ease of use, and extensive documentation make it a popular choice for developers working with images in Python.
from PIL import Image, ImageDraw
# Example: Drawing a simple shape on an image using Pillow
image = Image.new("RGB", (200, 200), "white")
draw = ImageDraw.Draw(image)
draw.rectangle([50, 50, 150, 150], outline="black", width=2)
image.show()
This code snippet demonstrates how to draw a simple shape (a rectangle) on an image using the Pillow library in Python. Let’s break it down step by step:
from PIL import Image, ImageDraw
: This line imports the necessary classes from the Pillow library. Image
is used to create and manipulate images, while ImageDraw
provides methods for drawing on images.image = Image.new("RGB", (200, 200), "white")
: Creates a new RGB image with dimensions of 200 pixels in width and 200 pixels in height. The image is initially filled with the color white.draw = ImageDraw.Draw(image)
: Creates an ImageDraw
object associated with the image created in the previous step. This object provides methods for drawing shapes and text on the image.draw.rectangle([50, 50, 150, 150], outline="black", width=2)
: Draws a rectangle on the image. The rectangle is defined by its top-left corner coordinates (50, 50) and bottom-right corner coordinates (150, 150). The outline
parameter sets the color of the rectangle’s outline to black, and the width
parameter specifies the width of the outline as 2 pixels.image.show()
: Displays the image with the drawn rectangle in a separate window using the default image viewer associated with the operating system.In summary, this code creates a new image with a white background, draws a black-outlined rectangle on it, and then displays the image with the drawn rectangle in a window using the default image viewer.
The output of the above code example will be a window displaying a white square with a black-outlined rectangle drawn inside it. The rectangle will be positioned with its top-left corner at coordinates (50, 50) and its bottom-right corner at coordinates (150, 150). The outline of the rectangle will be black, and it will have a width of 2 pixels. The image will be displayed using the default image viewer associated with the operating system.