簡體   English   中英

如何在另一個圖像上合成一個帶有alpha通道的圖像(RGB,沒有alpha通道)? 使用python PIL

[英]How to compose an image with an alpha channel over another image(RGB, without alpha channel)? Using python PIL

我的問題與此相似: 使用Python Imaging Library(PIL),一個人如何在另一個圖像上組成一個帶有alpha通道的圖像? 我有兩張圖片,最上面的圖片帶有Alpha通道,最下面的一張沒有。 我想將頂部圖像放在底部圖像上,以生成新圖像,就像將它們分層渲染一樣。 我想用Python PIL做到這一點。 任何建議都是可取的,謝謝!

只需將A設置為“ 1”即可將RGB圖像擴展為RGBA:

rgba = np.dstack((rgb, np.ones(rgb.shape[:-1])))

然后使用您提到的compose方法。

如果您使用枕頭,則可以簡單地使用:

imRGB.putalpha(alpha)
composite = PIL.Image.alpha_composite(imRGB, im2RGBA)

我已經解決了我的問題,問題是RGBA圖像中的alpha通道的值是0或255,我只是將255更改為220,所以頂部圖像不會覆蓋底部圖像。 我的代碼如下:

def transPNG(srcImageName, dstImageName):
img = Image.open(srcImageName)
img = img.convert("RGBA")
datas = img.getdata()
newData = list()
for item in datas:
    if item[0] > 200 and item[1] > 200 and item[2] > 200:
        newData.append(( 255, 255, 255, 0))
    else:
        newData.append((item[0], item[1], item[2], randint(220, 220)))
img.putdata(newData)
img.save(dstImageName,"PNG")

暫無
暫無

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

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