Blend Modes for Image Processing

2.2.0 · active · verified Thu Apr 16

The `blend-modes` package provides a Python implementation of various blend modes commonly found in graphics software like Adobe Photoshop or GIMP. It enables blending different images or image layers to create a wide array of visual effects. The library is optimized for performance through NumPy vectorization. The current version is 2.2.0, released on October 16, 2024, indicating an active development and release cadence.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to prepare two dummy RGBA images (background and foreground) as NumPy float arrays (0.0-255.0) and apply the `soft_light` blend mode. The output is also a NumPy float array in the same format. In a real application, image loading and saving would typically be handled by libraries like Pillow or OpenCV.

import numpy as np
from blend_modes import soft_light

# Simulate loading images (typically done with Pillow or OpenCV)
# Images must be float arrays (0.0-255.0) and have shape (H, W, 4) for RGBA.
height, width = 100, 150

# Background image: a gradient
bg_img = np.zeros((height, width, 4), dtype=float)
bg_img[:, :, 0] = np.linspace(0, 255, width) # Red gradient
bg_img[:, :, 1] = np.linspace(0, 255, height).reshape(-1, 1) # Green gradient
bg_img[:, :, 2] = 100.0 # Blue channel constant
bg_img[:, :, 3] = 255.0 # Full opacity

# Foreground image: a circle
fg_img = np.zeros((height, width, 4), dtype=float)
y, x = np.ogrid[0:height, 0:width]
center_y, center_x = height // 2, width // 2
radius = min(height, width) // 3
mask = (x - center_x)**2 + (y - center_y)**2 < radius**2
fg_img[mask, 0] = 255.0 # Red circle
fg_img[mask, 3] = 150.0 # Partial opacity for foreground

opacity = 0.7 # Opacity of the foreground layer (0.0 to 1.0)

# Perform blending
blended_img = soft_light(bg_img, fg_img, opacity)

print("Blended image shape:", blended_img.shape)
print("Blended image min value:", blended_img.min())
print("Blended image max value:", blended_img.max())
# In a real application, you would save or display blended_img,
# e.g., using Pillow: Image.fromarray(blended_img.astype(np.uint8)).save('output.png')

view raw JSON →