Python Pillow - 增強銳度



銳度是定義照片清晰度和細節的關鍵因素,它也是強調紋理和視覺衝擊力的寶貴工具。

增強銳度是一種影像處理技術,用於增強影像的邊緣、細節和對比度,從而使其呈現更清晰、更生動的外觀。由於相機抖動、低解析度或壓縮等因素導致的模糊、噪點或失真影像,此過程可以顯著提高其質量和可見性。

增強影像銳度

在 Python Pillow 庫中,可以透過使用ImageEnhance.Sharpness()類來增強和調整影像的銳度。它允許您透過應用增強因子來調整影像的銳度。

以下是 ImageEnhance.Sharpness() 類的語法:

class PIL.ImageEnhance.Sharpness(image)

以浮點值表示的增強因子作為引數傳遞給公共的單一介面方法 enhance(factor)。此因子在調整影像銳度方面起著重要作用。使用 0.0 的因子會導致影像完全模糊,而 1.0 的因子則會提供原始影像。較高的值會增強影像的銳度。

以下是實現影像銳度增強步驟:

  • 使用ImageEnhance.Sharpness()類建立一個銳度增強器物件。

  • 然後使用enhance()方法將增強因子應用於增強器物件。

示例

以下示例演示如何透過指定因子來調整影像的銳度。

from PIL import Image, ImageEnhance

# Open an image file
image = Image.open('Images/butterfly1.jpg')

# Create a Sharpness object
Enhancer = ImageEnhance.Sharpness(image)

# Display the original image
image.show()

# Enhance the sharpness with a factor of 4.0
sharpened_image = enhancer.enhance(4.0)

# Display the sharpened image
sharpened_image.show()

輸出

輸入影像:

butterfly and flowers

輸出銳度增強後的影像:

sharpness image

示例

在此示例中,您可以透過將增強因子設定為接近零的值來獲得輸入影像的模糊版本。

from PIL import Image, ImageEnhance

# Open an image file
image = Image.open('Images/butterfly.jpg')

# Create a Sharpness object
enhancer = ImageEnhance.Sharpness(image)

# Display the original image
image.show()

# Decrease the sharpness by using a factor of 0.05
sharpened_image = enhancer.enhance(0.05)

# Display the sharpened image
sharpened_image.show()

輸出

輸入影像:

butterfly and flowers

輸出輸入影像的模糊版本:

blured butterfly
廣告