如何在Python中使用枕头在PNG上覆盖图案

2024-03-29 12:15:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用枕头Python在衬衫上覆盖一个特定的图案,这是我的代码-

design =Image.open("source/stripe.png").convert("RGBA")
shirtsketchtrans = Image.open("source/shirtsketchtrans.png").convert("RGBA")

design.paste(shirtsketchtrans, (0,0), shirtsketchtrans)
design.show()

结果是这样的- I don't mind the size, that can be fixed

我真的不在乎尺寸,可以定下来。 但我想做的是在我的衬衫PNG上只覆盖条纹图案,而不是在整个背景中,基本上只在我的衬衫上覆盖条纹图案,而不是在其他任何地方

有什么解决办法吗? 感谢您的帮助!谢谢

编辑:输入/源图像-

enter image description here

enter image description here


Tags: 代码imagesourceconvertpngopenstripepaste
1条回答
网友
1楼 · 发布于 2024-03-29 12:15:02

PIL本身不够聪明,不知道衬衫的“内部”或“外部”是什么。您需要创建一个Transparency Mask,然后使用^{}组合它们

例如:

design = Image.open("source/stripe.png").convert("RGBA")
shirt_sketch_trans = Image.open("source/shirtsketchtrans.png").convert("RGBA")
shirt_sketch_mask = Image.open("source/shirtsketchmask.png").convert("RGBA")

full_design = Image.composite(design, shirt_sketch_trans, shirt_sketch_mask)
full_design.show()

相关问题 更多 >