小编not*_*ing的帖子

如何使用 Pillow 在 python 中使用黑白图像将图像屏蔽到另一个图像?

我正在尝试使用黑白图像来掩盖 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")
Run Code Online (Sandbox Code Playgroud)

头像1

阿凡达2

面具

预期结果

python mask python-imaging-library

5
推荐指数
1
解决办法
3142
查看次数

标签 统计

mask ×1

python ×1

python-imaging-library ×1