想要两张图当做两个图层叠加起来,很自然会想到用Image.paste() 把一张图贴到另一张图上,于是有这么两张图![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/f55c7d48f7335b9a5a4f0085dbd13b61.png)
想把img1 贴到 img2 上层,那么码起来
from PIL import Image
import os.path as op
path = r'c:\lalala\desktop'
img1 = Image.open(op.join(path, 'img1.png')).convert('RGBA')
img2 = Image.open(op.join(path, 'img2.png')).convert('RGBA')
img2.paste(img1, (0, 0))
img2.show()
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d09ab7dde9818285fbd2e53d57b76ee5.png)
诶嘿,跟想的不一样,表急,稍微改一下就好
img1 = Image.open(op.join(path, 'img1.png')).convert('RGBA')
img2 = Image.open(op.join(path, 'img2.png')).convert('RGBA')
r, g, b, a = img1.split()
img2.paste(img1, (0, 0), mask=a)
img2.show()
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/84db4418e6fff85007160abf2d0f6045.png)
没错,成了它