FocusStacker
FocusStacker is a high-performance Python library for advanced focus stacking using Laplacian pyramid merging. It combines multiple images taken at different focus distances to create a single image with extended depth of field.
Features
- Advanced Laplacian Pyramid Merging: Uses sophisticated algorithms for optimal focus stacking
- SIFT-based Alignment: Automatic image alignment using Scale-Invariant Feature Transform
- Memory Efficient: Optimized for processing large images with minimal memory usage
- Configurable Parameters: Fine-tune alignment and blending parameters
- Resource Monitoring: Built-in memory and disk space monitoring
- Comprehensive Error Handling: Detailed exceptions for debugging
- Type Safety: Full type annotations for better development experience
Installation
Using pip
pip install focusstacker
Using uv
uv add focusstacker
Quick Start
Basic Usage
from focusstacker import stack_images
# Define your input images
image_paths = [
"images/focus_1.jpg",
"images/focus_2.jpg",
"images/focus_3.jpg"
]
# Stack the images
stack_images(
image_paths=image_paths,
destimation_image_path="output/stacked_image.jpg"
)
Advanced Usage
from focusstacker import stack_images
from focusstacker.common.enums import AlignerChoice, BlenderChoice
stack_images(
image_paths=[
"images/focus_1.jpg",
"images/focus_2.jpg",
"images/focus_3.jpg",
"images/focus_4.jpg"
],
destimation_image_path="output/high_quality_stack.jpg",
aligner=AlignerChoice.SIFT,
blender=BlenderChoice.LAPLACIAN_PYRAMID_BALANCED,
levels=6 # More pyramid levels for higher quality
)
API Reference
stack_images()
The main function for focus stacking.
Parameters
image_paths(list[str | Path]): List of input image paths (2-50 images)destimation_image_path(str | Path): Output path for the stacked imagealigner(AlignerChoice, optional): Alignment algorithm (default:SIFT)blender(BlenderChoice, optional): Blending algorithm (default:LAPLACIAN_PYRAMID_BALANCED)levels(int, optional): Number of pyramid levels 3-8 (default: 5)
Supported Formats
- Input: JPEG (.jpg, .jpeg)
- Output: JPEG (.jpg, .jpeg)
Configuration Options
Alignment Algorithms
SIFT (Scale-Invariant Feature Transform)
- Best for: General photography, landscapes, macro
- Features: Robust to scale, rotation, and illumination changes
- Performance: High accuracy, moderate speed
Blending Algorithms
Laplacian Pyramid Balanced
- Best for: Most photography scenarios
- Features: Soft blending, natural transitions
- Performance: Balanced quality and speed
Laplacian Pyramid Max Sharpness
- Best for: Technical photography, maximum detail
- Features: Aggressive sharpness selection
- Performance: Highest detail, slower processing
Pyramid Levels
- 3-4 levels: Fast processing, good for previews
- 5-6 levels: Balanced quality and speed (recommended)
- 7-8 levels: Highest quality, slower processing
Error Handling
FocusStacker provides comprehensive error handling with specific exception types:
from focusstacker.common.exceptions import (
FocusStackerValidationException,
FocusStackerAlignmentException,
FocusStackerStackingException,
FocusStackerFileException,
FocusStackerMemoryException
)
try:
stack_images(
image_paths=["img1.jpg", "img2.jpg"],
destimation_image_path="result.jpg"
)
except FocusStackerValidationException as e:
print(f"Validation error: {e}")
except FocusStackerAlignmentException as e:
print(f"Alignment failed: {e}")
except FocusStackerStackingException as e:
print(f"Stacking failed: {e}")
except FocusStackerMemoryException as e:
print(f"Insufficient memory: {e}")
except FocusStackerFileException as e:
print(f"File operation failed: {e}")
Exception Types
FocusStackerValidationException: Input validation errorsFocusStackerAlignmentException: Image alignment failuresFocusStackerStackingException: Blending/stacking failuresFocusStackerFileException: File I/O errorsFocusStackerMemoryException: Memory/resource issuesFocusStackerImageProcessingException: Image processing errorsFocusStackerDirectoryException: Directory operation errors
Best Practices
Image Preparation
- Consistent Lighting: Use consistent lighting across all images
- Stable Camera: Use a tripod or stable surface
- Overlapping Focus: Ensure focus ranges overlap between images
- Image Quality: Use high-quality, sharp images
- File Format: Use JPEG format for best compatibility
Performance Optimization
- Image Size: Resize large images before processing if memory is limited
- Pyramid Levels: Use fewer levels for faster processing
- Batch Processing: Process multiple stacks sequentially
- Memory Monitoring: Monitor system resources during processing
System Requirements
Minimum Requirements
- Python 3.9+
- 4GB RAM
- 1GB free disk space
Recommended Requirements
- 8GB+ RAM
- 5GB+ free disk space
- SSD storage for better performance
Dependencies
numpy>=1.24.4opencv-contrib-python>=4.12.0.88psutil>=7.1.0pydantic>=2.11.9
Troubleshooting
Common Issues
Memory Errors
FocusStackerMemoryException: Insufficient memory
Solution: Reduce image size or use fewer pyramid levels
Alignment Failures
FocusStackerAlignmentException: Alignment failed
Solution: Ensure images have sufficient overlap and features
File Access Errors
FocusStackerFileException: Could not load image
Solution: Check file paths and permissions
Performance Tips
- Use SSD storage for temporary files
- Close other applications during processing
- Monitor system resources with task manager
- Use appropriate pyramid levels for your needs
Development Setup
# Clone the repository
git clone https://github.com/yourusername/focus-stacking.git
cd focus-stacking
# Install development dependencies
uv install --dev
# Run tests
uv run pytest
# Run type checking
uv run ty check
# Run linting
uv run ruff check
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
- OpenCV for computer vision algorithms
- NumPy for numerical computations
- The focus stacking community for inspiration and feedback
Changelog
v0.1.0
- Initial release
- SIFT-based alignment
- Laplacian pyramid blending
- Memory-efficient processing
- Comprehensive error handling
Metadata
Release files for focusstacker 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| focusstacker-0.1.0.tar.gz | 13.0 MB | With provenance | Details for focusstacker-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| focusstacker-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for focusstacker-0.1.0-py3-none-any.whl |
Total release size: 13.1 MB
Release files / focusstacker-0.1.0.tar.gz
| Download URL | focusstacker-0.1.0.tar.gz |
|---|---|
| Size | 13.0 MB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c727a48e428a001cc8f253109e499df22f94fcbdaedd4318b34fbe8d10ac9f5b
|
|
BLAKE2b-256 checksum How to use checksums |
c3d77f7c0742e8ebb7508808e1db5cb91981d301443002707cfb7d6b4d32deea
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 6, 2025.
Transparency logRelease files / focusstacker-0.1.0-py3-none-any.whl
| Download URL | focusstacker-0.1.0-py3-none-any.whl |
|---|---|
| Size | 31.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2db39102d5f24971a769208037dd2add19ec3a5195d9cd65c03c44f9ef6514fc
|
|
BLAKE2b-256 checksum How to use checksums |
43a8d81349f46158ddfb30abc9334c346685f09b6aaab6cb7566217219a0e329
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 6, 2025.
Transparency logSecurity for focusstacker 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for focusstacker-0.1.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 6, 2025.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.