Python Pillow - ImageChops.subtract_modulo() Function



The PIL.ImageChops.subtract_modulo function is used to subtract two images without clipping the result. Instead of clipping values below zero like the subtract() function, this subtract_modulo operation wraps around, similar to modulo arithmetic.

The following formula gives the mathematical representation of this operation −

$$\mathrm{out\:=\:((image1\:-\:image2)\%\:MAX}$$

This operation is reversible, meaning that the original pixel values can be reconstructed from the result.

Syntax

Following is the syntax of the function −

PIL.ImageChops.subtract_modulo(image1, image2)

Parameters

Here are the details of this function parameters −

  • image1 − This is the first input image.

  • image2 − This is the second input image.

Return Value

This function returns the Image object.

Examples

Example 1

In this example, the ImageChops.subtract_modulo() function is applied to two random RGB images (image1 and image2) created by using NumPy arrays. In the output, we can observe the pixel values of the two input images, and the output image at a specified location using the getpixel() function.

from PIL import Image, ImageChops
import numpy as np

# Create two random RGB images
image1 = Image.fromarray(np.array([(235, 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([(255, 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)))

# Subtract the two images without clipping the result
result = ImageChops.subtract_modulo(image1, image2)
print("Pixel values of the result at (0, 0) after subtract_modulo:", result.getpixel((0, 0)))

Output

Pixel values of image1 at (0, 0): (235, 0, 0)
Pixel values of image2 at (0, 0): (255, 0, 0)
Pixel values of the result at (0, 0) after subtract_modulo: (236, 0, 0)

Example 2

In this example, the ImageChops.subtract_modulo() function is used on two JPEG image files to add the images without clipping the result.

from PIL import Image, ImageChops

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

# Subtract the second image from the first without clipping the result
result = ImageChops.subtract_modulo(image1, image2)

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

Output

Input Image 1

tp logo

Input Image 2

rose

Output Image

imagechops subtract modulo

Example 3

Here is an example that uses the ImageChops.subtract_modulo() function on two PNG image files to add the images without clipping the result.

from PIL import Image, ImageChops

# Open two image files
image1 = Image.open('Images/pillow-logo.png')
image2 = Image.open('Images/test_img.png')

# Subtract the second image from the first without clipping the result
result = ImageChops.subtract_modulo(image1, image2)

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

Output

Input Image 1

pillow logo S

Input Image 2

test img

Output Image

chops subtract modulo
python_pillow_function_reference.htm
Advertisements