python实现同一文件夹下全部图像叠置操作并另存输出

from PIL import Image
import os


def pic_connect(base_layer_path, folders_path, save_path):
    # 加载底图(全黑图像作为底图)
    base_layer = Image.open(base_layer_path)
    # 三通道转四通道
    base_layer = base_layer.convert('RGBA')
    # 读取文件夹内文件
    folders = os.listdir(folders_path)
    # 逐个处理文件夹内文件
    for folder in folders:
        # 存储当前图像路径
        folder_path = os.path.join(folders_path, folder)
        # 加载当前遍历的图像
        layer = Image.open(folder_path)
        # 三通道转四通道
        layer = layer.convert('RGBA')
        # 获取遍历像素尺寸
        heigh, width = layer.size
        for i in range(heigh):
            for j in range(width):
                r, g, b, a = layer.getpixel((i, j))
                if r == 0:
                    layer.putpixel((i, j), (0, 0, 0, 0))  # 背景透明显示
                else:
                    layer.putpixel((i, j), (128, 0, 0, 255))  # 非背景区域显示为蓝色
        base_layer.paste(layer, (0, 0), layer)  # 贴图操作
    base_layer.save(save_path + "/" + "connect.png")  # 图片保存


if __name__ == "__main__":
    base_layer_path = r''
    folders_path = r''
    save_path = r''
    pic_connect(base_layer_path,folders_path,save_path)

你可能感兴趣的:(深度学习,python,开发语言,后端)