PILKit
PILKit is a collection of utilities and processors built on top of the Python Imaging Library (PIL), primarily Pillow. It simplifies common image manipulation tasks by providing an easy-to-use API for operations like resizing, adjusting, and cropping. The current stable version is 3.0, released in September 2023, and the library maintains an active development cadence.
Warnings
- breaking There is no explicit changelog or migration guide available between major versions, specifically from 2.x to 3.x. Users upgrading major versions may need to inspect the codebase for changes.
- gotcha PILKit relies on Pillow (or the deprecated PIL) for its core image handling. Pillow is a peer dependency and must be installed separately (e.g., `pip install Pillow`) for PILKit to function correctly.
- gotcha When saving processed images, direct use of `PIL.Image.save()` can sometimes lead to 'Suspension not allowed here' errors with certain image formats or operations. PILKit provides `pilkit.utils.save_image` to gracefully handle these common issues.
Install
-
pip install pilkit -
pip install Pillow
Imports
- ResizeToFit
from pilkit.processors import ResizeToFit
- ProcessorPipeline
from pilkit.processors import ProcessorPipeline
- prepare_image
from pilkit.processors import prepare_image
from pilkit.utils import prepare_image
- save_image
from PIL.Image import save
from pilkit.utils import save_image
Quickstart
import os
from PIL import Image
from pilkit.processors import ResizeToFit
from pilkit.utils import save_image
# Create a dummy image for demonstration
try:
img = Image.new('RGB', (200, 150), color = 'red')
img.save('original_image.png')
except ImportError:
print("Pillow not installed. Please install it with 'pip install Pillow'.")
exit()
# Define a processor
processor = ResizeToFit(width=100, height=100, upscale=False)
# Process the image
original_image = Image.open('original_image.png')
processed_image = processor.process(original_image)
# Save the processed image using pilkit's utility
save_image(processed_image, 'processed_image.png', 'PNG')
print("Original image saved as original_image.png")
print("Processed image (resized to fit 100x100) saved as processed_image.png")
# Clean up (optional)
os.remove('original_image.png')
os.remove('processed_image.png')