示例程序如下:
from PIL import Image, ImageDraw, ImageFont
import os
# 输入文件夹路径和图片P的路径
folder_path = r"运行结果文件夹"
image_p_path = r"水印.jpg"
output_folder_path = r"运行截图文件夹"
# 确保输出文件夹存在
if not os.path.exists(output_folder_path):
os.makedirs(output_folder_path)
# 遍历文件夹下的每张图片
for i in range(1, 7):
image_path = os.path.join(folder_path, f"{i}.png")
# 打开原始图片和图片P
image = Image.open(image_path)
image_p = Image.open(image_p_path)
# 获取图片尺寸和字体大小
width, height = image.size
font_size = int(height / 50)
# 将图片P粘贴到右下角
image.paste(image_p, (width - image_p.width, height - image_p.height))
# 在左下角绘制文字
draw = ImageDraw.Draw(image)
font = ImageFont.truetype("arial.ttf", font_size)
text = "2024.1.1 8:01:56"
text_width, text_height = draw.textsize(text, font)
text_position = (0, height - text_height)
draw.text(text_position, text, font=font, fill=(0, 0, 0))
# 保存修改后的图片到输出文件夹
output_path = os.path.join(output_folder_path, f"{i}.png")
image.save(output_path)
# 关闭图片
image.close()
image_p.close()
print("图片处理完成!")