Python Pillow - ImageChops.add() Function



The PIL.ImageChops.add() function performs the addition operation on two input images (image1 and image2). After the addition, the result is divided by a specified scale and then an offset is added. The mathematical representation of this operation is given by the formula −

$$\mathrm{out\:=\:((image1 + image2)/scale\:+\:offset)}$$

It's important to note that ImageChops.add operations are not reversible. This is primarily due to the data type being uint8, which can't represent negative values and is limited to a range of 0 to 255. When using ImageChops.add, if the resulting value exceeds 255, it is clipped or truncated to 255. This limitation makes the operation irreversible, as the original pixel values cannot be accurately recovered after clipping.

Syntax

Following is the syntax of the function −

PIL.ImageChops.add(image1, image2, scale=1.0, offset=0)

Parameters

Here are the details of this function parameters −

  • image1 − This is the first input image to add to another image.

  • image2 − This is the second input image to add to the first image.

  • scale (optional, default value: 1.0) − Scale is a factor by which the result of adding image1 and image2 is divided. If you omit this parameter, it defaults to 1.0.

  • offset (optional, default value: 0.0) − Offset is a value added to the result after the division by the scale. If you don't provide an offset value, it defaults to 0.0.

Return Value

This Function returns the Image object.

Examples

Example 1

In this example, two random RGB images (image1 and image2) are created using NumPy arrays. Then the ImageChops.add function is applied to see how the pixel values of the two images are added in the output image.

from PIL import Image, ImageChops
import numpy as np

# Create two random RGB images
image1 = Image.fromarray(np.array([(64, 64, 3), (255, 0, 0), (255, 255, 0), (255, 255, 255), (164, 0, 3)]), mode="RGB")
print("Pixel values of image1 at (0, 0):", image1.getpixel((0, 0)))

image2 = Image.fromarray(np.array([(200, 14, 3), (25, 222, 0), (255, 155, 0), (255, 55, 100), (180, 0, 78)]), mode="RGB")
print("Pixel values of image2 at (0, 0):", image2.getpixel((0, 0)))

# Add the two images
result = ImageChops.add(image1, image2)
print("Pixel values of the result at (0, 0) after addition:", result.getpixel((0, 0)))

Output

Pixel values of image1 at (0, 0): (64, 0, 0)
Pixel values of image2 at (0, 0): (200, 0, 0)
Pixel values of the result at (0, 0) after addition: (255, 0, 0)

We can see that, the output at (0, 0) is (255, 0, 0), as the sum (64 + 200) is clipped to 255.

Example 2

In this first example, we use the ImageChops.add function to add two images with the default parameters (scale=1.0 and offset=0.0).

from PIL import Image, ImageChops

# Open two image files
image1 = Image.open('Images/Rose_.jpg')
image2 = Image.open('Images/TP logo.jpg')

# Add the two images with default parameters (scale=1.0, offset=0.0)
result = ImageChops.add(image1, image2)

# Display the input and resulting images
image1.show()
image2.show()
result.show()

Output

Input Image 1

rose

Input Image 2

tp logo

Output Image

chop and add two images

Example 3

In this example, we add the two input images using the ImageChops.add function with a specific scale (5.0) and offset (2) values.

from PIL import Image, ImageChops

# Open two image files
image1 = Image.open('Images/Rose_.jpg')
image2 = Image.open('Images/TP logo.jpg')
 
# Add the two images a specific scale and offset values
result = ImageChops.add(image1, image2, scale=5.0, offset=2)

# Display the input and resulting images
image1.show()
image2.show()
result.show()

Output

Input Image 1

rose

Input Image 2

tp logo

Output Image

image with specific scale
python_pillow_function_reference.htm
Advertisements