mumuck/0打工人/矿工项目/萌宠/特效/C1769/PNG序列特效/0 GIF 图片水印,原始大小 -水印和添加文件名 - 50.py
2025-02-26 10:55:59 +08:00

67 lines
2.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import os
from PIL import Image, ImageDraw, ImageFont
# 获取脚本文件所在的目录
script_directory = os.path.dirname(os.path.abspath(__file__))
# 定义GIF预览文件夹路径
gif_preview_directory = os.path.join(script_directory, "000GIF预览")
os.makedirs(gif_preview_directory, exist_ok=True) # 创建文件夹,如果不存在则创建
def create_gif(frames, folder_name, duration=50, font_size=20):
images = []
for frame in frames:
image = Image.open(frame)
# 添加水印
watermark_image_path = "E:/path/shuiyin.png" # 水印图片路径
watermark_image = Image.open(watermark_image_path)
# 计算水印的大小宽度为GIF宽度的10%,保持宽高比)
gif_width, gif_height = image.size
watermark_width = int(gif_width * 0.35)
aspect_ratio = watermark_image.width / watermark_image.height
watermark_height = int(watermark_width / aspect_ratio)
watermark_image = watermark_image.resize((watermark_width, watermark_height))
# 创建一个新图像,带有黑色背景
new_image = Image.new("RGB", image.size, (0, 0, 0))
# 将水印放在新图像上
x = (gif_width - watermark_width) // 2
y = (gif_height - watermark_height) // 2
new_image.paste(watermark_image, (x, y))
# 将主图像叠加在水印上
new_image.paste(image, (0, 0), image)
# 添加文件夹名称
draw = ImageDraw.Draw(new_image)
# 使用自定义字体和大小
# font_path = "path/to/font.ttf" # 如果您有特定的字体文件,请使用它
font = ImageFont.truetype("arial.ttf", font_size) # 使用 Arial 字体,或者您的字体文件路径
text_x = int(gif_width * 0.05) # X位置为5%的宽度
text_y = int(gif_height * 0.05) # Y位置为5%的高度
draw.text((text_x, text_y), folder_name, font=font, fill=(255, 255, 255)) # 白色字体
images.append(new_image)
# 构建GIF文件名
gif_output_path = os.path.join(gif_preview_directory, f"{folder_name}.gif")
images[0].save(gif_output_path, save_all=True, append_images=images[1:], optimize=False, duration=duration, loop=0)
def process_folder(folder_path):
png_files = [file for file in os.listdir(folder_path) if file.lower().endswith(".png")]
if png_files:
folder_name = os.path.basename(folder_path) # 获取文件夹名称
create_gif([os.path.join(folder_path, file) for file in png_files], folder_name, duration=50, font_size=20) # 字体大小设为 20
# 当前目录
current_directory = os.getcwd()
# 处理当前目录下的所有文件夹及其子文件夹
for root, dirs, files in os.walk(current_directory):
process_folder(root)
print("处理完成!")