Pillow ImageChops Module
ImageChops is a submodule of Python PIL (Python Imaging Library), specifically used for image channel operations (abbreviated as "Channel Operations").
The ImageChops module provides a series of methods that can perform mathematical and logical operations on images, commonly used in scenarios such as image compositing and special effects processing.
Module Features
- All methods accept two images as parameters and operate on their corresponding pixels.
- Return a new image object.
- Most methods require the input images to have the same size and mode.
- Especially suitable for image compositing, blending, and special effects processing.
Common ImageChops Methods
The following are the most commonly used methods in the ImageChops module and their descriptions:
| Method Name | Description | Mathematical Expression |
|---|---|---|
| add(image1, image2, scale=1.0, offset=0) | Add two images. | out = ((image1 + image2) / scale + offset) |
| subtract(image1, image2, scale=1.0, offset=0) | Subtract two images. | out = ((image1 - image2) / scale + offset) |
| lighter(image1, image2) | Compare two images and take the lighter value of each pixel. | out = max(image1, image2) |
| darker(image1, image2) | Compare two images and take the darker value of each pixel. | out = min(image1, image2) |
| multiply(image1, image2) | Multiply two images. | out = image1 * image2 / 255 |
| screen(image1, image2) | Apply screen blending to two images. | out = 255 - (255 - image1) * (255 - image2) / 255 |
| overlay(image1, image2) | Overlay two images. | Choose multiply or screen operation based on pixel values. |
| difference(image1, image2) | Return the absolute difference between two images. | out = abs(image1 - image2) |
| invert(image) | Invert an image (negative effect). | out = 255 - image |
| logical_and(image1, image2) | Perform logical AND operation on two images. | out = image1 & image2 |
| logical_or(image1, image2) | Perform logical OR operation on two images. | out = image1 | image2 |
| logical_xor(image1, image2) | Perform logical XOR operation on two images. | out = image1 ^ image2 |
| blend(image1, image2, alpha) | Blend two images with a fixed transparency. | out = image1 * (1.0 - alpha) + image2 * alpha |
| composite(image1, image2, mask) | Composite two images using a mask image. | Select pixels from image1 or image2 based on a mask. |
| offset(image, xoffset, yoffset=None) | Offset image. | Shift the image by specified pixels in the x and y directions. |
Usage Examples
Basic Usage
Example
from PIL import Image, ImageChops
# Open two images
image1 = Image.open("image1.jpg")
image2 = Image.open("image2.jpg")
# Ensure both images have the same size
if image1.size == image2.size:
# Add the two images
added_image = ImageChops.add(image1, image2)
added_image.save("added.jpg")
# Get the difference between two images
diff_image = ImageChops.difference(image1, image2)
diff_image.save("difference.jpg")
# Open two images
image1 = Image.open("image1.jpg")
image2 = Image.open("image2.jpg")
# Ensure both images have the same size
if image1.size == image2.size:
# Add the two images
added_image = ImageChops.add(image1, image2)
added_image.save("added.jpg")
# Get the difference between two images
diff_image = ImageChops.difference(image1, image2)
diff_image.save("difference.jpg")
Creating an Image Negative
Example
from PIL import Image, ImageChops
# Open an image
original = Image.open("photo.jpg")
# Create a negative
inverted = ImageChops.invert(original)
inverted.save("negative.jpg")
# Open an image
original = Image.open("photo.jpg")
# Create a negative
inverted = ImageChops.invert(original)
inverted.save("negative.jpg")
Image Blending
Example
from PIL import Image, ImageChops
# Open two images
foreground = Image.open("foreground.png")
background = Image.open("background.jpg")
# Ensure they have the same size
if foreground.size == background.size:
# Blend images (alpha=0.5 means each takes 50%)
blended = ImageChops.blend(foreground, background, 0.5)
blended.save("blended.jpg")
# Open two images
foreground = Image.open("foreground.png")
background = Image.open("background.jpg")
# Ensure they have the same size
if foreground.size == background.size:
# Blend images (alpha=0.5 means each takes 50%)
blended = ImageChops.blend(foreground, background, 0.5)
blended.save("blended.jpg")
Notes
- Image mode: Most ImageChops methods require the input images to have the same mode (e.g., RGB, L, etc.)
- Image size: The two images being operated on must have the same dimensions.
- Return value: All methods return a new Image object and do not modify the original images.
- Performance considerations: For large images, these operations may consume a significant amount of memory.
- Special modes: Certain methods (such as logical operations) are typically used for 1-bit images (mode "1").
By using the ImageChops module appropriately, you can easily achieve various image processing effects, from simple image compositing to complex special effects.
Other Extensions