Python Pillow - ImageChops.multiply() 函式



PIL.ImageChops.multiply() 函式將兩張影像疊加在一起。如果將影像與純黑色影像相乘,則結果為黑色。如果將其與純白色影像相乘,則原始影像不受影響。

它適用於您希望對模式不為“1”的影像執行邏輯 AND 操作的場景。這可以透過將黑白蒙版作為第二張影像提供給 `multiply()` 函式來實現。

操作定義如下:

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

語法

以下是函式的語法:

PIL.ImageChops.multiply(image1, image2)

引數

以下是此函式引數的詳細資訊:

  • image1 - 第一個輸入影像。

  • image2 - 第二個輸入影像。

返回值

此函式的返回型別為 Image。

示例

示例 1

以下示例演示了 ImageChops.multiple() 函式的工作原理。

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)))

輸出

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

示例 2

在此示例中,PIL.ImageChops.multiply() 函式用於將影像與另一影像疊加。

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()

輸出

輸入影像 1

tajmahal birds

輸入影像 2

dotted butterfly

輸出影像

imagechops multiply

示例 3

在此示例中,PIL.ImageChops.multiply() 函式用於疊加兩張 PNG 影像。

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()

輸出

輸入影像 1

pillow logo w

輸入影像 2

color dots

輸出影像

chops multiply
python_pillow_function_reference.htm
廣告