我想截一张图,比如说:
使用另一张具有透明背景的图片,如下所示:
并得到如下结果:
如何使用 Python PIL/Pillow 实现此目的?或者任何其他库,但它必须是Python 的。
我们打电话吧source.png
到第一张图像,并且mask.png
到徽标™
你们的标志是透明的,但是是倒转的,所以透明度对我们来说没有用。另外,对于这种情况,当我们删除透明度时,透明区域几乎变成白色,因此我们需要对其进行阈值处理。
from PIL import Image, ImageOps
# Convert to grayscale
mask = Image.open('mask.png').convert('L')
# Threshold and invert the colors (white will be transparent)
mask = mask.point(lambda x: x < 100 and 255)
# The size of the images must match before apply the mask
img = ImageOps.fit(Image.open('source.png'),mask.size)
img.putalpha(mask) # Modifies the original image without return
img.save('result.png')
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)