Skip to main content

Advanced focus stacking using Laplacian pyramid merging

Project description

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

Recommended Requirements

  • 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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

focusstacker-0.1.0.tar.gz (13.0 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

focusstacker-0.1.0-py3-none-any.whl (31.8 kB view details)

Uploaded Python 3

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

Hashes for focusstacker-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c727a48e428a001cc8f253109e499df22f94fcbdaedd4318b34fbe8d10ac9f5b
MD5 e210887b1187fec2038bb233845c5e90
BLAKE2b-256 c3d77f7c0742e8ebb7508808e1db5cb91981d301443002707cfb7d6b4d32deea

See more details on using hashes here.

Provenance

The following attestation bundles were made for focusstacker-0.1.0.tar.gz:

Publisher: publish.yml on mdarwish228/FocusStacker

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for focusstacker-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2db39102d5f24971a769208037dd2add19ec3a5195d9cd65c03c44f9ef6514fc
MD5 cc89954d93483a2e9ae7f2faeba67335
BLAKE2b-256 43a8d81349f46158ddfb30abc9334c346685f09b6aaab6cb7566217219a0e329

See more details on using hashes here.

Provenance

The following attestation bundles were made for focusstacker-0.1.0-py3-none-any.whl:

Publisher: publish.yml on mdarwish228/FocusStacker

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page