Registry / type-stubs / types-pillow

types-pillow

JSON →
library10.2.0.20240822pypypi✓ verified 25d ago

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.

pip install types-pillow
INSTALL
IMPORT
SIG · TYPES-PILLOW
T
types-pillow
type-stubspythonv10.2.0.20240822
Install
1.6s avg
Import
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v10.2.0.20240822 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 18.2MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.6s · import 0.000s · 19MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Image
from PIL import Image
from PIL-stubs import Image

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)
Debug
Known issues
breakingPillow versions 10.3.0 and newer include their type annotations directly within the Pillow package itself (via `py.typed`). If you are using Pillow 10.3.0 or a later version, you MUST uninstall `types-Pillow` to avoid duplicate type definitions and potential type-checking conflicts.
fix
If `Pillow >= 10.3.0` is installed: `pip uninstall types-pillow`
affects: Pillow >= 10.3.0, types-Pillow (all versions when used with Pillow >= 10.3.0)
gotchaThe version of `types-Pillow` should ideally match the major.minor version of your installed `Pillow` library to ensure accurate type checking. For example, `types-Pillow==10.2.*` provides stubs for `Pillow==10.2.*`. Mismatched versions can lead to incorrect type hints or type-checking errors.
fix
Align your `types-Pillow` version with your `Pillow` version in your `requirements.txt` or `pyproject.toml` (e.g., `Pillow==10.2.0` and `types-Pillow==10.2.0.YYYYMMDD`).
affects: All versions
gotchaIn `Pillow > 9.1.0`, image filter constants (like `PIL.Image.LANCZOS`) were deprecated in favor of the `PIL.Image.Resampling` enum (e.g., `PIL.Image.Resampling.LANCZOS`). If you're using a newer `Pillow` (and `types-Pillow` that reflects this change) but your code still uses the old constants, type checkers will report errors.
fix
Update your code to use the `PIL.Image.Resampling` enum for image filters, e.g., `img.resize(size, PIL.Image.Resampling.LANCZOS)` instead of `img.resize(size, PIL.Image.LANCZOS)`.
affects: Pillow > 9.1.0, types-Pillow > 9.1.x.YYYYMMDD
Upgrade
Version history
10.2.0.20240822latest on PyPI · released Aug 22, 2024
Audit
Dependencies
PillowrequiredThis package provides type stubs for the Pillow library. While not a runtime dependency of 'types-pillow' itself, it is required for your application to actually use the types provided. Ensure version compatibility, e.g., types-Pillow==10.2.* targets Pillow==10.2.*.
Agent activity
38 hits · last 30 days
node
36
OpenAI (training)
1
Resources
types-pillow — pip install types-pillow · libregistry