Skip to main content

FocusStacker

Python Version License

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 image
  • aligner (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 errors
  • FocusStackerAlignmentException: Image alignment failures
  • FocusStackerStackingException: Blending/stacking failures
  • FocusStackerFileException: File I/O errors
  • FocusStackerMemoryException: Memory/resource issues
  • FocusStackerImageProcessingException: Image processing errors
  • FocusStackerDirectoryException: Directory operation errors

Best Practices

Image Preparation

  1. Consistent Lighting: Use consistent lighting across all images
  2. Stable Camera: Use a tripod or stable surface
  3. Overlapping Focus: Ensure focus ranges overlap between images
  4. Image Quality: Use high-quality, sharp images
  5. File Format: Use JPEG format for best compatibility

Performance Optimization

  1. Image Size: Resize large images before processing if memory is limited
  2. Pyramid Levels: Use fewer levels for faster processing
  3. Batch Processing: Process multiple stacks sequentially
  4. Memory Monitoring: Monitor system resources during processing

System Requirements

Minimum Requirements

  • Python 3.9+
  • 4GB RAM
  • 1GB free disk space
  • 8GB+ RAM
  • 5GB+ free disk space
  • SSD storage for better performance

Dependencies

  • numpy>=1.24.4
  • opencv-contrib-python>=4.12.0.88
  • psutil>=7.1.0
  • pydantic>=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

  1. Use SSD storage for temporary files
  2. Close other applications during processing
  3. Monitor system resources with task manager
  4. 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)

Source distribution for focusstacker 0.1.0
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)

Table of built distributions (wheels) for focusstacker 0.1.0
File Interpreter ABI Platform Actions
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 log

Release 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 log

Security 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.

Select from 2 files

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 log

A 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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page