Pillow is a powerful and user-friendly Python Imaging Library (PIL) fork, designed to provide image processing capabilities to Python applications. It is the de facto standard library for image manipulation in Python, offering extensive support for various image file formats and a wide range of image processing operations. Pillow is maintained and actively developed, improving upon the original PIL which had stagnated.
Key features and capabilities of Pillow include:
1. Image Format Support: It can open, manipulate, and save many different image formats, including common ones like JPEG, PNG, GIF, BMP, TIFF, ICO, WebP, and more obscure formats.
2. Image Manipulation: Provides functions for essential image transformations such as resizing, rotating, cropping, flipping (horizontal/vertical), and transposing images.
3. Drawing Capabilities: Allows drawing primitives like lines, rectangles, ellipses, arcs, chords, and polygons onto images. It also supports rendering text with various fonts.
4. Color Conversions: Facilitates converting images between different color modes (e.g., RGB to grayscale, RGB to RGBA, RGB to CMYK, etc.).
5. Image Filtering: Offers a variety of built-in filters (e.g., blur, sharpen, emboss, smooth, contour, edge enhance) and the ability to apply custom convolutional kernels.
6. Pixel Manipulation: Enables direct access to pixel data, allowing for intricate pixel-level modifications.
7. Image Composition: Supports combining multiple images, pasting one image onto another, or blending images.
8. Thumbnail Generation: Easily create thumbnails from larger images.
Pillow is widely used in web development for handling user-uploaded images, in data science for image preprocessing, in graphic design for automated image tasks, and in many other domains where image handling is required. Its intuitive API and comprehensive features make it an indispensable tool for Python developers working with images.
Installation:
`pip install Pillow`
Basic Usage:
The primary class in Pillow is `Image` from the `PIL` module. All image operations are performed on `Image` objects.
Example Code
from PIL import Image, ImageFilter, ImageDraw, ImageFont
import os
--- 1. Create a dummy image for demonstration ---
In a real scenario, you would open an existing image:
try:
img = Image.open('input_image.jpg')
except FileNotFoundError:
print("input_image.jpg not found. Creating a blank image for demonstration.")
img = Image.new('RGB', (400, 300), color = (73, 109, 137))
For self-contained example, let's create a blank image
img = Image.new('RGB', (600, 400), color = (73, 109, 137))
print(f"Original image size: {img.size}")
--- 2. Resize the image ---
new_size = (300, 200)
resized_img = img.resize(new_size)
print(f"Resized image size: {resized_img.size}")
resized_img.save('output_resized.jpg')
--- 3. Rotate the original image ---
rotated_img = img.rotate(45, expand=True) expand=True adjusts canvas size to fit rotated image
print(f"Rotated image size: {rotated_img.size}")
rotated_img.save('output_rotated.png')
--- 4. Apply a filter (e.g., Blur) to the original image ---
blurred_img = img.filter(ImageFilter.BLUR)
blurred_img.save('output_blurred.jpg')
--- 5. Crop the original image ---
Define the crop box as (left, upper, right, lower)
crop_box = (50, 50, 350, 250) Crops a 300x200 rectangle
cropped_img = img.crop(crop_box)
print(f"Cropped image size: {cropped_img.size}")
cropped_img.save('output_cropped.jpg')
--- 6. Draw text and a rectangle on the image ---
Create a drawing object
draw = ImageDraw.Draw(img)
Define text and position
text = "Hello, Pillow!"
text_position = (10, 10)
text_color = (255, 255, 255) White
Try to load a font, fall back to default if not found
try:
Using a common font that might be available
font = ImageFont.truetype("arial.ttf", 30)
except IOError:
font = ImageFont.load_default() Use default bitmap font
draw.text(text_position, text, fill=text_color, font=font)
Draw a rectangle
rectangle_coords = (50, 50, 200, 100)
outline_color = (255, 0, 0) Red
fill_color = (0, 0, 255, 128) Blue with some transparency (RGBA)
Note: drawing with fill color on 'RGB' image will convert alpha to solid.
For proper transparency, image should be 'RGBA'. Let's convert if needed.
if img.mode != 'RGBA':
img = img.convert('RGBA')
draw = ImageDraw.Draw(img) Recreate draw object if image mode changed
draw.rectangle(rectangle_coords, outline=outline_color, fill=fill_color)
--- 7. Save the final modified image ---
output_filename = 'output_final_modified.png'
img.save(output_filename)
print(f"Final modified image saved as '{output_filename}'")
--- Optional: Display the images (requires a GUI environment) ---
img.show() Shows the final modified image
resized_img.show()
rotated_img.show()
blurred_img.show()
cropped_img.show()
Clean up (optional) - remove generated files
for f in ['output_resized.jpg', 'output_rotated.png', 'output_blurred.jpg', 'output_cropped.jpg', output_filename]:
if os.path.exists(f):
os.remove(f)








Pillow