将没有背景的 RGB 图像粘贴到另一张图片上 (PIL)

Paste RBG image without its background onto another picture (PIL)

所以我需要 post 将 RGB 图片(具有透明背景)添加到背景图像上,但是当我尝试 post 它时 image.paste( ) 我得到了透明背景,通过它我看不到我想要的背景。我该怎么办?

谢谢!

代码(仅此而已):

        skin = Image.open("./temp2.png")
        skin = skin.resize((148, 355))
        stats.paste(skin,(42,232))

        stats.save("temp.png")

图片:

Image I want to paste

The background I want to paste it on

Wanted Result

Actual Result (gray is transparent)

您可以在 PIL 中使用 alpha composite 来粘贴图像并保留 alpha 通道以实现透明度。

stats = Image.open("stats.png")
skin = Image.open("skin.png")
skin = skin.resize((148, 355))
stats.alpha_composite(skin,(42,232))

stats.save("temp.png")

结果图像: