1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
from PIL import Image, ImageDraw, ImageFont import os import configparser
ini_file_path = 'user.ini'
config = configparser.ConfigParser() config.read(ini_file_path)
font_path = config['diy']['path'] font_size = int(config['diy']['size']) text = config['diy']['text'] font_color = tuple(map(int, config['diy']['color'].split(',')))
input_dir = config['directories']['input'] output_dir = config['directories']['out']
x = int(config['xy']['x']) y = int(config['xy']['y'])
font = ImageFont.truetype(font_path, size=font_size)
if not os.path.exists(output_dir): os.makedirs(output_dir)
for filename in os.listdir(input_dir): if filename.lower().endswith(('.png', '.jpg', '.webp')): image_path = os.path.join(input_dir, filename) image = Image.open(image_path) width, height = image.size
text_bbox = ImageDraw.Draw(image).textbbox((0, 0), text, font=font) text_x = width - text_bbox[2] - x text_y = height - text_bbox[3] - y
draw = ImageDraw.Draw(image) draw.text((text_x, text_y), text, font=font, fill=font_color)
output_path = os.path.join(output_dir, filename) image = image.convert('RGB') image.save(output_path)
print(f"{output_path}")
print('批量处理完成!')
|