
- 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 - 處理 Alpha 通道
影像中的 Alpha 通道是一個表示每個畫素透明度或不透明度的元件。它通常用於 RGBA 影像,其中每個畫素由其紅色、綠色、藍色和 Alpha 值定義。此通道決定了影像中顯示多少背景。簡單來說,我們可以說 Alpha 值為 0 的畫素完全透明,而 Alpha 值為255的畫素完全不透明。
以下是具有不同透明度級別的影像的視覺表示 -

以下是與 Pillow 中 Alpha 通道相關的關鍵概念的細分 -
RGBA 模式
具有 Alpha 通道的影像通常以RGBA模式表示。
RGBA影像中的每個畫素都有四個元件,即紅色、綠色、藍色和 Alpha。
透明度級別
Alpha 通道值通常在 0 到 255 之間。
0表示完全透明,即完全透明或不可見。
255表示完全不透明,即完全不透明或不可見。
訪問 Alpha 通道
我們可以使用 Pillow Image 模組的split()函式訪問 Alpha 通道。此函式用於分離影像的各個顏色通道。它將影像分解為其組成通道,使我們能夠分別訪問和操作每個通道。
使用split()函式分割通道提供了分別處理和操作各個顏色或 Alpha 通道的靈活性,從而可以進行精確的影像調整和處理。
當在影像物件上呼叫 split() 函式時,它會返回一個包含各個通道影像的元組。對於 RGBA 影像,元組包含紅色、綠色、藍色和 Alpha 通道。對於灰度或單通道影像,split() 函式仍然返回四個相同通道的相同副本。透過專門訪問 Alpha 通道,您可以直接操作影像的透明度。
以下是 split() 函式的語法和引數 -
r,g,b,a = image.split()
其中,
image - 這是使用 Image.open() 函式開啟的影像物件。
r, g, b, a - 這些變數分別接收表示紅色、綠色、藍色和 Alpha(即透明度)通道的單獨波段。
示例
在此示例中,我們使用split()函式來分割和訪問 Alpha 通道。
from PIL import Image # Open an image img_obj = Image.open('Images/colordots.png') #Split the image into RGBA channels red, green, blue, alpha = img_obj.split() # Alpha (transparency) channel alpha.show()
將要使用的輸入 RGBA 影像

輸出 Alpha 通道

建立具有 Alpha 通道的影像
要建立具有 Alpha 通道的影像,我們可以使用new()函式。此函式提供諸如模式、寬度、高度和用於自定義影像的 RGBA 值之類的選項。最後一個引數的最後一個值定義影像的透明度,即 Alpha。
示例
在此示例中,我們將使用new()函式建立具有 Alpha 通道的影像。
from PIL import Image # Create an RGBA image with Semi-transparent green image = Image.new('RGBA', (700, 300), (10, 91, 51, 100)) # Display the resultant image with alpha channel image.show()
執行上述程式後,您將獲得如下所示的 RGBA 輸出 -

修改 Alpha 通道
可以使用putpixel()函式設定畫素的各個 Alpha 值,以對影像的 Alpha 通道應用修改。
Python Pillow 的putpixel()函式用於更改影像中指定位置的單個畫素的顏色。此函式允許我們透過提供其座標和所需顏色來直接修改特定畫素的顏色。
以下是 putpixel() 函式的語法和引數。
image.putpixel((x, y), color)
image - 這是使用 Image.open() 函式開啟的影像物件。
(x, y) - 這些是我們想要設定顏色的畫素的座標。x 表示水平位置,y 表示垂直位置。
color - 我們想要分配給指定畫素的顏色值。顏色值的格式取決於影像模式。
示例
在此示例中,我們使用 Pillow 庫的putpixel()函式修改給定輸入影像的畫素的 Alpha 值。
from PIL import Image # Open an image image = Image.open("Images/colordots.png") width, height = image.size # Modify alpha values of pixels for y in range(height): for x in range(width): r, g, b, a = image.getpixel((x, y)) image.putpixel((x, y), (r, g, b, int(a * 0.5))) # Reduce alpha by 50% # Display the modified image image.show()
輸入影像

輸出修改後的影像

注意
putpixel() 函式直接修改記憶體中的影像。它對於對單個畫素進行少量修改很有用。
對於大規模畫素操作或更復雜的處理,我們必須考慮使用其他 Pillow 函式,這些函式可以對更大的區域或整個影像進行操作,以獲得更好的效能。
使用 Alpha 通道合成影像
Image.alpha_composite()函式用於合成兩張具有 Alpha 通道的影像。此函式根據其 Alpha 通道混合影像。
示例
在此示例中,我們使用Image.alpha_composite()函式根據其 Alpha 通道混合兩張影像。
from PIL import Image # Open two images img1 = Image.open('Images/tp_logo.png') img2 = Image.open('Images/colordots_2.png') # Composite the images composite = Image.alpha_composite(img1, img2) # Display the result composite.show()
輸入影像 -


輸出 Alpha 合成影像 -

儲存具有 Alpha 通道的影像
為了保留透明度,請以支援 Alpha 通道的格式(如 PNG)儲存影像。JPEG 等格式不支援 Alpha 通道,並將丟棄透明度資訊。我們可以使用 Pillow 中提供的save()函式。
示例
在此示例中,我們使用Image.save()函式儲存具有 Alpha 通道的影像。
from PIL import Image # Create a new image with alpha channel new_img = Image.new('RGBA', (700, 300), (255, 0, 0, 128)) # Save the image with alpha channel new_img.save("output Image/new_img.png", "PNG") print("The image with alpha channel is saved.")
執行上述程式碼後,您將在當前工作目錄中找到生成的 PNG 檔案“new_img.png” -
The image with alpha channel is saved.