Python Imaging Library (Fork) providing comprehensive image processing capabilities for reading, writing, and manipulating images across dozens of formats.
—
Pixel-level mathematical operations between images including blending, compositing, arithmetic operations, and logical operations for advanced image processing workflows.
def add(image1, image2, scale=1.0, offset=0):
"""
Add two images with optional scaling and offset.
Parameters:
- image1 (Image): First image
- image2 (Image): Second image
- scale (float): Scale factor for result
- offset (int): Offset added to result
Returns:
Image: Result image
"""
def subtract(image1, image2, scale=1.0, offset=0):
"""
Subtract second image from first.
Parameters:
- image1 (Image): First image
- image2 (Image): Second image
- scale (float): Scale factor for result
- offset (int): Offset added to result
Returns:
Image: Result image
"""
def multiply(image1, image2):
"""
Multiply two images.
Parameters:
- image1 (Image): First image
- image2 (Image): Second image
Returns:
Image: Result image
"""def blend(image1, image2, alpha):
"""
Blend two images using alpha transparency.
Parameters:
- image1 (Image): First image
- image2 (Image): Second image
- alpha (float): Blend factor (0.0-1.0)
Returns:
Image: Blended image
"""
def composite(image1, image2, mask):
"""
Composite two images using a mask.
Parameters:
- image1 (Image): Background image
- image2 (Image): Foreground image
- mask (Image): Mask image
Returns:
Image: Composite image
"""def difference(image1, image2):
"""
Calculate absolute difference between images.
Parameters:
- image1 (Image): First image
- image2 (Image): Second image
Returns:
Image: Difference image with absolute difference values
"""
def darker(image1, image2):
"""
Take darker pixels from two images.
Parameters:
- image1 (Image): First image
- image2 (Image): Second image
Returns:
Image: Result with darker pixels (min values)
"""
def lighter(image1, image2):
"""
Take lighter pixels from two images.
Parameters:
- image1 (Image): First image
- image2 (Image): Second image
Returns:
Image: Result with lighter pixels (max values)
"""Photoshop-style blend modes for sophisticated image compositing.
def screen(image1, image2):
"""
Screen blend mode - inverted multiply effect.
Parameters:
- image1 (Image): Base image
- image2 (Image): Blend image
Returns:
Image: Screen blended result
"""
def soft_light(image1, image2):
"""
Soft light blend mode for subtle lighting effects.
Parameters:
- image1 (Image): Base image
- image2 (Image): Blend image
Returns:
Image: Soft light blended result
"""
def hard_light(image1, image2):
"""
Hard light blend mode for dramatic lighting effects.
Parameters:
- image1 (Image): Base image
- image2 (Image): Blend image
Returns:
Image: Hard light blended result
"""
def overlay(image1, image2):
"""
Overlay blend mode combining multiply and screen.
Parameters:
- image1 (Image): Base image
- image2 (Image): Blend image
Returns:
Image: Overlay blended result
"""Arithmetic operations without clipping, using modulo wrap-around.
def add_modulo(image1, image2):
"""
Add two images with modulo wrap-around (no clipping).
Parameters:
- image1 (Image): First image
- image2 (Image): Second image
Returns:
Image: Result with modulo addition
"""
def subtract_modulo(image1, image2):
"""
Subtract two images with modulo wrap-around (no clipping).
Parameters:
- image1 (Image): First image
- image2 (Image): Second image
Returns:
Image: Result with modulo subtraction
"""Boolean operations for binary (mode "1") images.
def logical_and(image1, image2):
"""
Logical AND between two binary images.
Parameters:
- image1 (Image): First binary image (mode "1")
- image2 (Image): Second binary image (mode "1")
Returns:
Image: Logical AND result
Note:
Both images must be mode "1" (binary)
"""
def logical_or(image1, image2):
"""
Logical OR between two binary images.
Parameters:
- image1 (Image): First binary image (mode "1")
- image2 (Image): Second binary image (mode "1")
Returns:
Image: Logical OR result
Note:
Both images must be mode "1" (binary)
"""
def logical_xor(image1, image2):
"""
Logical XOR between two binary images.
Parameters:
- image1 (Image): First binary image (mode "1")
- image2 (Image): Second binary image (mode "1")
Returns:
Image: Logical XOR result
Note:
Both images must be mode "1" (binary)
"""Basic image manipulation utilities.
def constant(image, value):
"""
Fill a channel with a constant gray level.
Parameters:
- image (Image): Template image for size
- value (int): Gray level value (0-255)
Returns:
Image: New grayscale image filled with constant value
"""
def duplicate(image):
"""
Create a copy of an image.
Parameters:
- image (Image): Source image
Returns:
Image: Copy of the source image
"""
def invert(image):
"""
Invert an image (photographic negative).
Parameters:
- image (Image): Source image
Returns:
Image: Inverted image
"""
def offset(image, xoffset, yoffset=None):
"""
Shift image pixels with wrap-around.
Parameters:
- image (Image): Source image
- xoffset (int): Horizontal offset in pixels
- yoffset (int): Vertical offset in pixels (defaults to xoffset)
Returns:
Image: Offset image with wrapped pixels
"""from PIL import Image, ImageChops
# Load two images
img1 = Image.open("image1.jpg")
img2 = Image.open("image2.jpg")
# Blend images
blended = ImageChops.blend(img1, img2, 0.5)
# Calculate difference
diff = ImageChops.difference(img1, img2)
# Add images
added = ImageChops.add(img1, img2, scale=0.5)
blended.save("blended.jpg")
diff.save("difference.jpg")
added.save("added.jpg")Install with Tessl CLI
npx tessl i tessl/pypi-pillow