Python Pillow - ImageChops.add_modulo() 函式



PIL.ImageChops.add_modulo 函式用於新增兩張影像,而不裁剪結果。與 add() 函式不同,該函式不會裁剪超過最大值 (MAX) 限制的值,而是進行迴圈運算,類似於模運算。

以下公式給出了此運算的數學表示:

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

與 ImageChops.add() 函式不同,此運算是可逆的,這意味著可以根據結果重建原始畫素值。

語法

以下是該函式的語法:

PIL.ImageChops.add_modulo(image1, image2)

引數

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

  • image1 - 這是要新增到另一張影像的第一個輸入影像。

  • image2 - 這是要新增到第一張影像的第二個輸入影像。

返回值

此函式返回 Image 物件。

示例

示例 1

在此示例中,使用 NumPy 陣列建立兩個隨機 RGB 影像 (image1 和 image2)。然後應用 ImageChops.add_modulo() 函式,以檢視在執行模加運算後,兩個影像的畫素值如何在輸出影像中表示。

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

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

輸出

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 add_modulo: (234, 0, 0)

示例 2

在此示例中,ImageChops.add_modulo() 函式用於兩個 JPEG 影像檔案,以新增影像而不裁剪結果。

from PIL import Image, ImageChops

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

# Add the two images without clipping the result
result = ImageChops.add_modulo(image1, image2)

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

輸出

輸入影像 1

tp logo

輸入影像 2

pillow logo S

輸出影像

imagechops add modulo

示例 3

這是一個使用 ImageChops.add_modulo() 函式對兩個 PNG 影像檔案新增影像而不裁剪結果的示例。

from PIL import Image, ImageChops

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

# Add the two images without clipping the result
result = ImageChops.add_modulo(image1, image2)

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

輸出

輸入影像 1

color dots

輸入影像 2

pillow w

輸出影像

chops add modulo
python_pillow_function_reference.htm
廣告