Pillow Imagechops Modules
ImageChops is a submodule of Python PIL (Python Imaging Library), specifically designed for image channel operations (short for "Channel Operations").
The ImageChops module provides a series of methods for performing 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
* Returns a new image object
* Most methods require input images to have the same size and mode
* Particularly 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 | Function 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, take the brighter value of each pixel | out = max(image1, image2) |
| darker(image1, image2) | Compare two images, take the darker value of each pixel | out = min(image1, image2) |
| multiply(image1, image2) | Multiply two images | out = image1 * image2 / 255 |
| screen(image1, image2) | Screen blend two images | out = 255 - (255 - image1) * (255 - image2) / 255 |
| overlay(image1, image2) | Overlay two images | Select multiply or screen operation based on pixel value |
| difference(image1, image2) | Return absolute difference of two images | out = abs(image1 - image2) |
| invert(image) | Invert image (negative effect) | out = 255 - image |
| logical_and(image1, image2) | Logical AND operation on two images | out = image1 & image2 |
| logical_or(image1, image2) | Logical OR operation on two images | out = image1 | image2 |
| logical_xor(image1, image2) | Logical XOR operation on two images | out = image1 ^ image2 |
| blend(image1, image2, alpha) | Blend two images with fixed transparency | out = image1 * (1.0 - alpha) + image2 * alpha |
| composite(image1, image2, mask) | Composite two images using mask | Select image1 or image2 pixels based on mask |
| offset(image, xoffset, yoffset=None) | Offset image | Move image by specified pixels in 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 two images have the same size
if image1.size== image2.size:
# Add two images
added_image = ImageChops.add(image1, image2)
added_image.save("added.jpg")
# Get difference between two images
diff_image = ImageChops.difference(image1, image2)
diff_image.save("difference.jpg")
### Create Image Negative
## Example
from PIL import Image, ImageChops
# Open image
original = Image.open("photo.jpg")
# Create 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 same size
if foreground.size== background.size:
# Blend images (alpha=0.5 means 50% each)
blended = ImageChops.blend(foreground, background,0.5)
blended.save("blended.jpg")
* * *
## Notes
1. **Image Mode**: Most ImageChops methods require input images to have the same mode (such as RGB, L, etc.)
2. **Image Size**: The two images being operated on must have the same dimensions
3. **Return Value**: All methods return new Image objects and do not modify the original images
4. **Performance Considerations**: For large images, these operations may consume significant memory
5. **Special Modes**: Certain methods (such as logical operations) are typically used for 1-bit images (mode "1")
By reasonably using the ImageChops module, you can easily achieve various image processing effects, from simple image compositing to complex special effects processing.
YouTip