
- Python Pillow 教程
- Python Pillow - 首頁
- Python Pillow - 概述
- Python Pillow - 環境設定
- 基本影像操作
- Python Pillow - 影像處理
- Python Pillow - 調整影像大小
- Python Pillow - 翻轉和旋轉影像
- Python Pillow - 裁剪影像
- Python Pillow - 為影像新增邊框
- Python Pillow - 識別影像檔案
- Python Pillow - 合併影像
- Python Pillow - 剪下和貼上影像
- Python Pillow - 滾動影像
- Python Pillow - 在影像上寫入文字
- Python Pillow - ImageDraw 模組
- Python Pillow - 合併兩張影像
- Python Pillow - 建立縮圖
- Python Pillow - 建立水印
- Python Pillow - 影像序列
- Python Pillow 顏色轉換
- Python Pillow - 影像上的顏色
- Python Pillow - 使用顏色建立影像
- Python Pillow - 將顏色字串轉換為 RGB 顏色值
- Python Pillow - 將顏色字串轉換為灰度值
- Python Pillow - 透過更改畫素值來更改顏色
- 影像處理
- Python Pillow - 降噪
- Python Pillow - 更改影像模式
- Python Pillow - 影像合成
- Python Pillow - 使用 Alpha 通道
- Python Pillow - 應用透視變換
- 影像濾鏡
- Python Pillow - 為影像新增濾鏡
- Python Pillow - 卷積濾鏡
- Python Pillow - 模糊影像
- Python Pillow - 邊緣檢測
- Python Pillow - 浮雕影像
- Python Pillow - 增強邊緣
- Python Pillow - 反銳化蒙版濾鏡
- 影像增強和校正
- Python Pillow - 增強對比度
- Python Pillow - 增強銳度
- Python Pillow - 增強顏色
- Python Pillow - 校正色彩平衡
- Python Pillow - 去噪
- 影像分析
- Python Pillow - 提取影像元資料
- Python Pillow - 識別顏色
- 高階主題
- Python Pillow - 建立動畫 GIF
- Python Pillow - 批次處理影像
- Python Pillow - 轉換影像檔案格式
- Python Pillow - 為影像新增填充
- Python Pillow - 顏色反轉
- Python Pillow 與 NumPy 結合使用
- Python Pillow 與 Tkinter BitmapImage 和 PhotoImage 物件結合使用
- Image 模組
- Python Pillow - 影像混合
- Python Pillow 有用資源
- Python Pillow - 快速指南
- Python Pillow - 函式參考
- Python Pillow - 有用資源
- Python Pillow - 討論
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...')
輸出
輸入影像

結果影像

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

輸入影像 2

輸出影像

示例 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

輸入影像 2

輸出影像

示例 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

輸入影像 2

輸出影像

python_pillow_function_reference.htm
廣告