如何使用 PIL 合併透明 PNG 圖片與其他圖片?


在影像處理領域,合併兩張或更多影像是一個常見的操作。一個常見的用例是將透明的 PNG 圖片與另一張圖片合併,以建立包含這兩張圖片的合成圖片。

在本文中,我們將學習如何使用 PIL 合併透明 PNG 圖片與另一張圖片。PIL 是一個功能強大的 Python 影像處理庫。它提供了一系列用於開啟、操作和儲存不同型別影像檔案的函式。該庫相容各種影像格式,包括 JPEG、PNG、BMP、GIF 和 TIFF。

為了使用 PIL 合併透明 PNG 圖片與另一張圖片,我們需要執行以下步驟:

  • 使用 Image.open() 函式將兩張圖片載入到 PIL 中。

  • 建立一個與背景圖片大小相同的新圖片。

  • 使用 paste() 函式將背景圖片貼上到新圖片上。

  • 使用 paste() 函式將前景圖片貼上到新圖片上,但使用一個掩碼來指示哪些畫素應該是透明的。

  • 使用 save() 函式將合併後的圖片儲存到檔案中。

現在讓我們詳細瞭解一下如何逐步使用 PIL 合併透明 PNG 圖片與另一張圖片。

步驟 1:將圖片載入到 PIL 中

第一步是將背景圖片和透明的 PNG 圖片載入到 PIL 中。我們可以使用 Image.open() 函式來實現,該函式將圖片的檔名作為引數。例如,要載入名為“background.jpg”的背景圖片和名為“foreground.png”的透明 PNG 圖片,我們可以使用以下程式碼:

from PIL import Image
background = Image.open("background.jpg")
foreground = Image.open("foreground.png")

步驟 2:建立一個與背景圖片大小相同的新圖片

接下來,我們需要建立一個與背景圖片大小相同的新圖片。我們可以透過呼叫 Image.new() 函式並傳入背景圖片的大小來實現。例如,如果背景圖片是 800x600 畫素,我們可以建立一個相同大小的新圖片,如下所示:

merged_image = Image.new("RGBA", background.size)

步驟 3:將背景圖片貼上到新圖片上

現在我們可以使用 paste() 函式將背景圖片貼上到新圖片上。此函式接受以下引數:

  • 要貼上的圖片(在本例中為背景圖片)。

  • 貼上圖片的位置(指定為 (x, y) 元組)。

  • 一個可選的掩碼,可用於控制貼上圖片的透明度。

例如,要將背景圖片貼上到新圖片的左上角,我們可以使用以下程式碼:

merged_image.paste(background, (0, 0)

步驟 4:使用掩碼將前景圖片貼上到新圖片上

現在我們需要將透明的 PNG 圖片貼上到新圖片上,但使用一個掩碼來指示哪些畫素應該是透明的。我們可以使用 split() 函式從前景圖片的 alpha 通道建立掩碼,如下所示:

_, _, _, mask = foreground.split()

步驟 5:將合併後的圖片儲存到檔案中

最後,我們可以使用 save() 函式將合併後的圖片儲存到檔案中。我們可以使用適當的引數指定檔名和檔案格式。例如,要將合併後的圖片儲存為名為“merged.jpg”的 JPEG 檔案,我們可以使用以下程式碼:

merged_image.save("merged.jpg", "JPEG")

示例

將透明 PNG 圖片與 JPEG 背景圖片合併

在下面的示例中,我們使用 Python 中的 PIL 庫將透明 PNG 圖片與 JPEG 背景圖片合併。它包括將圖片載入到 PIL 中,建立一個與背景圖片大小相同的新圖片,將背景圖片貼上到新圖片上,從前景圖片的 alpha 通道建立掩碼,並將前景圖片與掩碼一起貼上到新圖片上。最後,合併後的圖片被儲存到檔案中。

# import PIL module
from PIL import Image
# Load the images
myBackgroundImage = Image.open("yourbackground.jpg")
myForegroundImage = Image.open("yourforeground.png")
# Create a new image with the same size as the background image
myMerged_image = Image.new("RGBA", myBackgroundImage.size)
# Paste the background image onto the new image
myMerged_image.paste(myBackgroundImage, (0, 0))
# Create a mask from the alpha channel of the foreground image
_, _, _, mask = myForegroundImage.split()
# Paste the foreground image onto the new image with the mask
myMerged_image.paste(myForegroundImage, (0, 0), mask)
# Saving the merged image to a file
myMerged_image.save("yourmerged.jpg", "JPEG"

輸出

輸入圖片

輸出圖片

示例

合併多個透明 PNG 圖片

在下面的示例中,我們使用 Python 中的 PIL 庫合併了多個透明 PNG 圖片。它包括將圖片載入到 PIL 中,建立一個與背景圖片大小相同的新圖片,將背景圖片貼上到新圖片上,從每個前景圖片的 alpha 通道建立掩碼,並將每個前景圖片與其相應的掩碼一起貼上到新圖片上。最後,合併後的圖片被儲存到檔案中。

# import PIL module
from PIL import Image

# Loading the images
myBackgroundImage = Image.open("yourbackground.jpg")
myForegroundImage1 = Image.open("yourforeground1.png")
myForegroundImage2 = Image.open("yourforeground2.png")

# Create a new image with the same size as the background image
myMerged_image = Image.new("RGBA", myBackgroundImage.size)

# Paste the background image onto the new image
myMerged_image.paste(myBackgroundImage, (0, 0))

# Create a mask from the alpha channel of the foreground image
_, _, _, mask1 = myForegroundImage1.split()
_, _, _, mask2 = myForegroundImage2.split()

# Paste the foreground image onto the new image with the mask
myMerged_image.paste(myForegroundImage1, (0, 0), mask1)
myMerged_image.paste(myForegroundImage2, (0, 0), mask2)

# Saving the merged image to a file
myMerged_image.save("yourmerged.png", "PNG")

輸出

輸入圖片

輸出圖片

結論

將透明 PNG 圖片與另一張圖片合併是影像處理中的一項常見任務。在本文中,我們學習瞭如何使用 Python 影像庫 (PIL) 將透明 PNG 圖片與背景圖片合併。我們介紹了將圖片載入到 PIL 中、建立與背景圖片大小相同的新圖片、將背景圖片貼上到新圖片上、從前景圖片的 alpha 通道建立掩碼、將前景圖片與掩碼一起貼上到新圖片上以及將合併後的圖片儲存到檔案的步驟。使用這些技巧,您可以建立複雜的合成圖片,這些圖片組合了多個具有透明度的圖片。

更新於: 2023-08-31

2K+ 閱讀量

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告