Python Pillow - ImageMath.eval() 函式



Pillow 庫在其 ImageMath 模組中提供了一個名為 eval() 的函式,用於評估影像表示式。它允許您對影像執行算術、位運算和邏輯運算等操作。該模組支援標準 Python 表示式語法,並提供 Pillow 庫提供的附加函式。

PIL.ImageMath.eval() 函式在給定的環境中評估表示式。此函式支援標準 Python 表示式語法以及用於影像處理的其他函式。

需要注意的是,ImageMath 模組目前僅支援單層影像。要處理多波段影像,應使用 split() 方法或 merge() 函式。

語法

以下是函式的語法:

PIL.ImageMath.eval(expression, environment)

引數

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

  • expression - 表示 Python 表示式的字串。

  • environment - 字典或關鍵字引數,將影像名稱對映到 Image 例項。

返回值

該函式返回一個影像、一個整數值、一個浮點值或一個畫素元組,具體取決於表示式。

示例

示例 1

此示例演示如何使用 ImageMath 模組的 eval() 函式使用標準算術運算子評估表示式。

from PIL import Image, ImageMath

# Open an image and convert it to grayscale
image1 = Image.open("Images/black rose.jpg").convert('L')

# Evaluate the expression 'a + 100' to invert pixel values
resultant_image = ImageMath.eval('a + 100', a=image1)

# Display the original and resultant images
image1.show()
resultant_image.show()
print('Evaluated the expression with standard arithmetical operator successfully...')

輸出

輸入影像

black and white rose

結果影像

pil imagemath eval
Evaluated the expression with standard arithmetical operator successfully...

示例 2

這是一個示例,演示如何使用 eval() 函式使用位運算子評估表示式。

需要注意的是,位運算子不適用於浮點影像。

from PIL import Image, ImageMath

# Open and convert the first grayscale image
image1 = Image.open("Images/ColorDots.png").convert('L')

# Open and convert the second grayscale image
image2 = Image.open("Images/TP-W.png").convert('L')

# Apply bitwise AND operation on the two images
resultant_image = ImageMath.eval('a & b', a=image1, b=image2)

# Display the original images and the result
image1.show()
image2.show()
resultant_image.show()
print('Evaluated the expression of the Bitwise operator successfully...')

輸出

輸入影像 1

convert grayscale image

輸入影像 2

convert grayscale image

輸出影像

resultant grayscale image

示例 3

以下示例對整個影像應用邏輯“或”運算子。

from PIL import Image, ImageMath

# Open and convert the first grayscale image
image1 = Image.open("Images/ColorDots.png").convert('L')

# Open and convert the second grayscale image
image2 = Image.open("Images/TP-W.png").convert('L')

# Use the logical "or" operation on the entire images
resultant_image = ImageMath.eval('(a or b)', a=image1, b=image2)

# Display the original images and the result
image1.show()
image2.show()
resultant_image.show()

print('Evaluated the expression using the logical OR successfully...')

輸出

輸入影像 1

convert grayscale image

輸入影像 2

convert grayscale image

輸出影像

logical or on entire images

示例 3

以下示例使用 ImageMath.eval() 函式評估包含內建函式的表示式。

from PIL import Image, ImageMath

# Open and convert the first grayscale image
image1 = Image.open("Images/ColorDots.png").convert('L')

# Open and convert the second grayscale image
image2 = Image.open("Images/TP-W.png").convert('L')

# Evaluate the expression "min(a, b)" using ImageMath
resultant_image = ImageMath.eval('min(a, b)', a=image1, b=image2)

# Display the original images and the result
image1.show()
image2.show()
resultant_image.show()

print('Evaluated the expression using the Built-in Functions successfully...')

輸出

輸入影像 1

convert grayscale image

輸入影像 2

convert grayscale image

輸出影像

imagemath eval
python_pillow_function_reference.htm
廣告