Python实现:对D:\A文件夹中所有的jpg格式文件处理,为每一个jpg图片文件加水印,水印文件为png格式,水印文件位于D:\B文件夹中,水印文件有多个,遍历D:\B中水印文件叠加到D:\A文件夹中每一个JPG图片上去。结果输出:D:\ProcessedImages
是不是上面的意思啊?
[Python] 纯文本查看 复制代码 import os
from PIL import Image
def add_watermark(source_folder, watermark_folder, output_folder):
if not os.path.exists(output_folder):
os.makedirs(output_folder)
for image_name in os.listdir(source_folder):
if image_name.lower().endswith('.jpg'):
image_path = os.path.join(source_folder, image_name)
image = Image.open(image_path)
for watermark_name in os.listdir(watermark_folder):
if watermark_name.lower().endswith('.png'):
watermark_path = os.path.join(watermark_folder, watermark_name)
watermark = Image.open(watermark_path).convert("RGBA")
position = (10, 10)
width, height = watermark.size
image.paste(watermark, position, watermark)
output_path = os.path.join(output_folder, image_name)
image.save(output_path, 'JPEG')
print(f"Processed {image_name} with watermark {watermark_name}")
source_folder = 'D:\\A'
watermark_folder = 'D:\\B'
output_folder = 'D:\\ProcessedImages'
add_watermark(source_folder, watermark_folder, output_folder)
|