Advanced focus stacking using Laplacian pyramid merging
Project description
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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file focusstacker-0.1.0.tar.gz.
File metadata
- Download URL: focusstacker-0.1.0.tar.gz
- Upload date:
- Size: 13.0 MB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c727a48e428a001cc8f253109e499df22f94fcbdaedd4318b34fbe8d10ac9f5b
|
|
| MD5 |
e210887b1187fec2038bb233845c5e90
|
|
| BLAKE2b-256 |
c3d77f7c0742e8ebb7508808e1db5cb91981d301443002707cfb7d6b4d32deea
|
Provenance
The following attestation bundles were made for focusstacker-0.1.0.tar.gz:
Publisher:
publish.yml on mdarwish228/FocusStacker
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
focusstacker-0.1.0.tar.gz -
Subject digest:
c727a48e428a001cc8f253109e499df22f94fcbdaedd4318b34fbe8d10ac9f5b - Sigstore transparency entry: 584747479
- Sigstore integration time:
-
Permalink:
mdarwish228/FocusStacker@3c3b78ab8b18cd1f2086c441ec35e86479472ce6 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/mdarwish228
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@3c3b78ab8b18cd1f2086c441ec35e86479472ce6 -
Trigger Event:
push
-
Statement type:
File details
Details for the file focusstacker-0.1.0-py3-none-any.whl.
File metadata
- Download URL: focusstacker-0.1.0-py3-none-any.whl
- Upload date:
- Size: 31.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2db39102d5f24971a769208037dd2add19ec3a5195d9cd65c03c44f9ef6514fc
|
|
| MD5 |
cc89954d93483a2e9ae7f2faeba67335
|
|
| BLAKE2b-256 |
43a8d81349f46158ddfb30abc9334c346685f09b6aaab6cb7566217219a0e329
|
Provenance
The following attestation bundles were made for focusstacker-0.1.0-py3-none-any.whl:
Publisher:
publish.yml on mdarwish228/FocusStacker
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
focusstacker-0.1.0-py3-none-any.whl -
Subject digest:
2db39102d5f24971a769208037dd2add19ec3a5195d9cd65c03c44f9ef6514fc - Sigstore transparency entry: 584747481
- Sigstore integration time:
-
Permalink:
mdarwish228/FocusStacker@3c3b78ab8b18cd1f2086c441ec35e86479472ce6 -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/mdarwish228
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@3c3b78ab8b18cd1f2086c441ec35e86479472ce6 -
Trigger Event:
push
-
Statement type: