Willow Image Library

1.12.0 · active · verified Thu Apr 09

Willow is a Python image library that provides a unified API over multiple backend imaging libraries such as Pillow, Wand (ImageMagick), and OpenCV. It offers common image manipulation capabilities like resizing, cropping, and format conversion, allowing developers to switch backends with minimal code changes. The current version is 1.12.0, and it maintains a regular release cadence, often aligning with new Python and backend library versions.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to load, resize, and save an image using Willow. It automatically detects the best available backend (like Pillow) and performs common image manipulations. Ensure you have installed Willow with at least one backend (e.g., `pip install 'willow[Pillow]'`) for this code to run successfully.

import os
from willow.image import Image

# Create dummy image data for demonstration
# In a real application, you'd load from a file or network stream
dummy_image_data = b'GIF89a\x01\x00\x01\x00\x00\xff\x00,\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x00;' # Minimal valid GIF

try:
    # Load the image using Willow's automatic backend detection
    # Requires a backend like Pillow to be installed (e.g., pip install 'willow[Pillow]')
    image = Image.check(dummy_image_data)

    # Get original image properties
    print(f"Original format: {image.format}")
    print(f"Original size: {image.get_size()}")

    # Resize the image
    image.resize((50, 50))

    # Get new image properties
    print(f"Resized size: {image.get_size()}")

    # Save the processed image data (e.g., as JPEG)
    processed_data = image.save_as_jpeg()

    # Optionally, save to a file
    output_filename = 'processed_image.jpg'
    with open(output_filename, 'wb') as f:
        f.write(processed_data)
    print(f"Image saved to {output_filename}")

    # Clean up dummy file
    os.remove(output_filename)

except Exception as e:
    print(f"Error processing image: {e}")
    print("Please ensure a backend like Pillow is installed (e.g., pip install 'willow[Pillow]').")

view raw JSON →