Color Matcher

0.6.0 · active · verified Wed Apr 15

Color Matcher is a Python package (version 0.6.0) designed for efficient color transfer across images. It is highly useful for tasks like automatic color-grading of photographs, paintings, and film sequences, as well as image data augmentation in deep learning. The library implements various color mapping methods, including those based on Reinhard et al., Monge-Kantorovich Linearization (MKL) by Pitie et al., and an analytical solution to Multi-Variate Gaussian Distribution (MVGD) transfer. The project maintains an active development status with regular updates.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to load source and reference images (creating dummy ones for easy demonstration), perform color transfer using the `ColorMatcher` class's `transfer()` method, and save the resulting image. Images are handled as NumPy arrays.

import numpy as np
from color_matcher import ColorMatcher
from color_matcher.io_handler import load_img_file, save_img_file
import os
from PIL import Image # For creating dummy images

# Create dummy image files for demonstration if they don't exist
def create_dummy_image(filepath, color):
    img = np.full((100, 100, 3), color, dtype=np.uint8)
    Image.fromarray(img).save(filepath)

src_path = 'source_image.png'
ref_path = 'reference_image.png'
output_path = 'output_image.png'

# Ensure Pillow is installed if creating dummy images
try:
    from PIL import Image
except ImportError:
    print("Pillow not installed. Please install with 'pip install Pillow' to run this quickstart.")
    exit(1)

if not os.path.exists(src_path):
    create_dummy_image(src_path, [255, 0, 0]) # Red
if not os.path.exists(ref_path):
    create_dummy_image(ref_path, [0, 0, 255]) # Blue

# Load source and reference images
# Images are expected as NumPy arrays, typically (H, W, C) with values 0-255
img_src = load_img_file(src_path)
img_ref = load_img_file(ref_path)

# Initialize ColorMatcher
cm = ColorMatcher()

# Perform color transfer using the 'hm-mkl-hm' method (a robust compound method)
# Other methods include 'reinhard', 'mvgd', 'hm', 'mkl', etc.
img_transferred = cm.transfer(src=img_src, ref=img_ref, method='hm-mkl-hm')

# Save the result
save_img_file(img_transferred, output_path)

print(f"Color transfer complete. Output saved to {output_path}")

# Clean up dummy images (optional)
os.remove(src_path)
os.remove(ref_path)
os.remove(output_path)

view raw JSON →