Pillow ImageChops Module
ImageChops is a submodule of Python PIL (Python Imaging Library), specifically for image channel operations (short for "Channel Operations").
The ImageChops module provides a series of methods that can perform mathematical and logical operations on images, commonly used in image compositing, special effects processing, and other scenarios.
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
- Particularly suitable for image compositing, blending, and special effects processing
Common ImageChops Methods
The following are the most commonly used methods of the ImageChops module and their functional 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, taking the brighter value of each pixel | out = max(image1, image2) |
| darker(image1, image2) | Compare two images, taking 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 the 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 a logical AND operation on two images | out = image1 & image2 |
| logical_or(image1, image2) | Perform a logical OR operation on two images | out = image1 | image2 |
| logical_xor(image1, image2) | Perform a 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 the mask |
| offset(image, xoffset, yoffset=None) | Offset an image | Move an image by a specified number of 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 are the same size
if
image1.
size
==
image2.
size
:
Add two images
added_image
=
ImageChops.
add
(
image1
,
image2
)
added_image.
save
(
"added.jpg"
)
Get the difference between the two images
diff_image
=
ImageChops.
difference
(
image1
,
image2
)
diff_image.
save
(
"difference.jpg"
)
Create a negative of the image
Example
from
PIL
import
Image
,
ImageChops
Open 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 the same size
if
foreground.
size
==
background.
size
:
Blend the images (alpha=0.5 means each accounts for 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 (such as 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 image
- Performance considerations: For large images, these operations may consume more memory
- Special Modes: Some methods (such as logical operations) are usually used on 1-bit images (mode "1")
By using the ImageChops module reasonably, you can easily achieve various image processing effects, from simple image compositing to complex special effects processing.