Typing stubs for Pillow

10.2.0.20240822 · maintenance · verified Fri Apr 10

types-Pillow is a PEP 561 type stub package that provides external type annotations for the Pillow library. It enables static type checkers like mypy, pyright, and PyCharm to perform type checking on code that uses Pillow. This package specifically targets Pillow versions up to 10.2.x. Pillow versions 10.3.0 and newer now include their type annotations directly within the library, making this stub package unnecessary for those versions.

Warnings

Install

Imports

Quickstart

This example demonstrates how to use Pillow with type hints. When `types-pillow` is installed, a static type checker (like MyPy) will understand the types of `PIL.Image.Image` and its methods, helping to catch errors at development time. You interact with the `PIL` library directly, and `types-pillow` works in the background.

from PIL import Image
from typing import TYPE_CHECKING
import os

# The 'types-pillow' package provides type hints for Pillow
# You don't directly import from types_pillow for usage; it's picked up by type checkers.

def process_image(img_path: str) -> Image.Image:
    """Opens an image, converts it to grayscale, and returns it."""
    try:
        with Image.open(img_path) as img:
            # Type checkers will use the stubs provided by types-pillow
            grayscale_img: Image.Image = img.convert("L")
            return grayscale_img
    except FileNotFoundError:
        print(f"Error: Image at {img_path} not found.")
        # In a real application, you might raise an error or return a default image.
        # For demonstration, we'll return a new blank image.
        return Image.new("L", (1, 1))

if __name__ == "__main__":
    # Create a dummy image for the example if it doesn't exist
    dummy_image_path = "test_image.jpg"
    try:
        Image.new("RGB", (100, 100), color='red').save(dummy_image_path)
    except Exception:
        pass # Ignore if it fails, e.g., in a read-only environment

    processed = process_image(dummy_image_path)
    print(f"Processed image mode: {processed.mode}, size: {processed.size}")
    # processed.show() # Uncomment to display the image (might open an external viewer)

    # Clean up dummy image
    if os.path.exists(dummy_image_path):
        os.remove(dummy_image_path)

view raw JSON →