Python Pillow - ImageChops.multiply() Function



The PIL.ImageChops.multiply() function superimposes two images on top of each other. If you multiply an image with a solid black image, the result is black. If you multiply it with a solid white image, the original image is unaffected.

It is useful for scenarios where you want to perform a logical AND operation on an image with a mode other than "1". This can be achieved by providing a black-and-white mask as the second image to the `multiply()` function.

The operation is defined as follows −

$$\mathrm{out\:=\:image1\:*\:image2\:/\:MAX}$$

Syntax

Following is the syntax of the function −

PIL.ImageChops.multiply(image1, image2)

Parameters

Here are the details of this function parameters −

  • image1 − The first input image.

  • image2 − The second input image.

Return Value

The return type of this function is an Image.

Examples

Example 1

Here is an example that demonstrates the working of the ImageChops.multiple() function.

from PIL import Image, ImageChops
import numpy as np

# Create two binary images with mode "1"
array1 = np.array([(154, 64, 3), (255, 0, 0), (255, 255, 0), (255, 255, 255), (164, 0, 3)], dtype=np.uint8)
array2 = np.array([(200, 14, 3), (20, 222, 0), (255, 155, 0), (255, 55, 100), (180, 0, 78)], dtype=np.uint8)

image1 = Image.fromarray(array1)
image2 = Image.fromarray(array2)

# Display the pixel values of the two input images
print("Pixel values of image1 at (0, 0):", image1.getpixel((0, 0)))
print("Pixel values of image2 at (0, 0):", image2.getpixel((0, 0)))

# Superimpose the two images using multiply
result = ImageChops.multiply(image1, image2)

# Display the pixel values of the resulting image at (0, 0)
print("Pixel values of the result at (0, 0) after multiply:", result.getpixel((0, 0)))

Output

Pixel values of image1 at (0, 0): 154
Pixel values of image2 at (0, 0): 200
Pixel values of the result at (0, 0) after multiply: 120

Example 2

In this example, the PIL.ImageChops.multiply() function is used to superimpose an image with another image.

from PIL import Image, ImageChops

# Open two image files
image1 = Image.open('Images/Tajmahal_2.jpg')
image2 = Image.open('Images/black-doted-butterflies.jpg')

# Superimpose the image with another using the multiply function
result = ImageChops.multiply(image1, image2)

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

Output

Input Image 1

tajmahal birds

Input Image 2

dotted butterfly

Output Image

imagechops multiply

Example 3

In this example, the PIL.ImageChops.multiply() function is used to superimpose two PNG images.

from PIL import Image, ImageChops

# Open an image file
image1 = Image.open('Images/pillow-logo-w.png')

# Create a black-and-white mask
image2 = Image.open('Images/ColorDots.png')

# Superimpose the image with the mask
result = ImageChops.multiply(image1, image2)

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

Output

Input Image 1

pillow logo w

Input Image 2

color dots

Output Image

chops multiply
python_pillow_function_reference.htm
Advertisements