功能说明

使用 Pillow 库为指定文件夹内的 PNG 图片批量添加水印。水印图片的路径为 watermark_path,将水印图片叠加到原图左上角位置,处理后的图片会保存在原图同文件夹下,文件名在原文件名基础上添加 -watermark 后缀。

运行环境

  • Python 3.x
  • Pillow 图像处理库

源代码

from os import path
from os import walk as walk
from PIL import Image

image_paths, input_paths = [], []
watermark_path = r'D:\experiment\watermark.png'

def add_watermark(image_path, watermark_path, output_path):
    image = Image.open(image_path)
    watermark = Image.open(watermark_path)
    mask = watermark.copy()
    output = image.copy()
    output.paste(watermark, (0, 0), mask)
    output.save(output_path)

for _, _, files in walk('image'):
    for file in files:
        if path.splitext(file)[-1] in ('.png', '.PNG'):
            image_paths.append( ( path.join('image', file), *path.splitext(file) ) )

for image in image_paths:
    input_paths.append( ( image[0], 'image\\{}-watermark{}'.format(image[1], image[2]) ) )

for image, output in input_paths:
    add_watermark(image, watermark_path, output)
    print('{} Completed!'.format(image))