from PIL import Image
#目标地址
target='原来图片.jpg'
#二值图地址
decorate='装饰图片.jpg'
class ImageCut:
def cover(self, target, decorate):
# 分别打开2张图片
image_target = Image.open(target)
image_decorate = Image.open(decorate)
# todo 校验图片长宽相等
image_target_width = image_target.width
image_target_height = image_target.height
image_decorate_width = image_decorate.width
image_decorate_height = image_decorate.height
# 对比像素点
for h in range(image_target_height):
for w in range(image_target_width):
# 打印该图片的所有点
# print(image_decorate.getpixel((w, h)), end=" ")
# 判断像素点是否为约定好的 255, 255, 255--白色
if (image_decorate.getpixel((w, h)) !=(255, 255, 255)):
# 设定目标图片像素点为 255, 255, 255
image_target.putpixel((w, h), (255, 255, 255))
print('\n')
# 保存处理后的图片
image_target.save('---生成后的图片---.jpg')
#调用
ImageCut().cover(target,decorate)
遍历像素点,能实现功能但是效率很低。