我正在尝试使用黑白图像来遮盖image1的某些区域,并使用Pillow将其粘贴到python中的image2上。我尝试使用“ PIL.Image.composite(image1,image2,mask)”,但是它没有用,或者我做错了。抱歉,我没有代码了,我剩下的唯一代码是
from PIL import Image, ImageEnhance, ImageOps, ImageDraw, ImageFilter
import os
avatars = []
for img in os.listdir():
if img.endswith(".png") is True:
avatars.append(img)
#open the images
mask = image.open("./masks/roundmask.png")
avatar1 = Image.open(avatars[0]).resize((128,128))
avatar2 = Image.open(avatars[1]).resize((128,128))
"""
mask the image avatar1 using the mask image and paste it on top of avatar2
"""
end = Image.open("./template/image.png").paste(avatar1, (190,93)).paste(avatar2, (420,38))
end.save("./finished/end.png")
我只能猜测您是尝试使用不兼容的图像尺寸(它们的尺寸都略有不同),还是模式错误。无论如何,从这2个输入图像和此蒙版开始:
这是您需要的:
#!/usr/bin/env python3
from PIL import Image
# Load images, discard pointless alpha channel, set appropriate modes and make common size
av1 = Image.open('avatar1.png').convert('RGB').resize((500,500))
av2 = Image.open('avatar2.png').convert('RGB').resize((500,500))
mask = Image.open('mask.png').convert('L').resize((500,500))
# Composite and save
Image.composite(av1,av2,mask).save('result.png')
关键字:Python,图像处理,PIL,枕头,合成,蒙版,蒙版,混合。