簡體   English   中英

使用 PIL/Pillow 在背景上粘貼透明圖像

[英]Pasting transparent image over background using PIL/Pillow

我正在嘗試使圖像部分透明,然后將其疊加在第二張圖像上。 例如,獲取此圖像並將其設置為 50% 透明度,然后將其覆蓋在此圖像上以創建此合成圖像 我嘗試使用以下代碼執行此操作:

from PIL import Image

background = Image.open("one.png")
foreground = Image.open("circle.png")
foreground.putalpha(120) # Sets the oval to 50% transparency
background.paste(foreground, (0, 0)) # Paste oval over the background
background.show()

但是,當我嘗試這個時,它只顯示前景。 是否可以通過這種方式使用 PIL/Pillow 調整透明度以實現所需的合成圖像?

我不知道“putalpha”是如何工作的,但是對於用透明背景覆蓋 png 圖像的類似問題,我剛剛在課程中學到的是 png 圖像也必須是掩碼。 像這樣:

from PIL import Image

background = Image.open("one.png")
foreground = Image.open("circle.png")
foreground.putalpha(120) # Sets the oval to 50% transparency
background.paste(foreground, (0, 0),mask=foreground) 
# Paste oval over the background
# use oval as mask
background.show()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM