简体   繁体   English

Python PIL:将透明图像混合到另一个上

[英]Python PIL: Blend transparent image onto another

I need to blend an image over another image using Pythons PIL Library. 我需要使用Pythons PIL Library将图像混合到另一个图像上。

As you can see in the image below, my two source images are A and B. When I do: 如下图所示,我的两个源图像是A和B.当我这样做时:

imageA.paste(imageB, (0, 0), imageB)

I get C as a result, but the part at the top of the gray background is now transparent. 我得到了C作为结果,但灰色背景顶部的部分现在是透明的。 Image D is what I get when I put B over A in Photoshop and is what I need to achieve with PIL. 图像D是我在Photoshop中将B放在A上时得到的,是我需要用PIL实现的。

What am I doing wrong? 我究竟做错了什么? How can I compose B over A in PIL to get D instead of C? 如何在PIL中用A组合B来获得D而不是C?

示例图像

I can't comment for now (rep constraint). 我现在不能评论(rep约束)。

But I think what you really need, according to your need, is to do this instead: 但我认为根据您的需要,您真正需要的是:

imageB.paste(imageA, (0, 0), imageA)

Basically, that is, make B the background image to get the desired results, because that's what I see in D. 基本上,也就是说,使B成为背景图像以获得所需的结果,因为这就是我在D中看到的。

EDIT: Looking around more, I found this: https://stackoverflow.com/a/15919897/4029893 编辑:环顾四周,我发现了这个: https//stackoverflow.com/a/15919897/4029893

I think you should definitely use the alpha_composite method, since paste doesn't work as expected for background images with transparency. 我认为你肯定应该使用alpha_composite方法,因为粘贴对于具有透明度的背景图像不起作用。

使用RGBA作为透明蒙版

imageA.paste(imageB, (0, 0), imageB.convert('RGBA'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM