利用纯文本文件的客人名单,创建定制的邀请函
#! python3
# 定制的座位卡.py - 利用纯文本文件的客人名单,创建定制的邀请函
import os
from PIL import Image,ImageDraw,ImageFont
# Create text objection and image objection.
textObj = open('guests.txt')
imgObj = Image.open('flower.jpg')
# Loop over all the guests' name.
for guestName in textObj.readlines():
# Create a cut.
imgCutObj = imgObj.copy().crop((0,0,288,360))
drawObj = ImageDraw.Draw(imgCutObj)
# Add outline
drawObj.line([(0,0),(288,0),(288,360),(0,360),(0,0)],'black',12)
# Add the guest name to the flower image.
fontsFolder = 'C:\\Windows\\Fonts'
bigFont = ImageFont.truetype(os.path.join(fontsFolder,'arial.ttf'),32)
drawObj.text((int(0.2 * imgCutObj.width) , int(0.5 * imgCutObj.height)),
text = guestName,
fill = 'CornflowerBlue',
font = bigFont)
# Save the image with a new name.
imgCutObj.save('To '+guestName[:-1]+'.png')
# Close the text.
textObj.close()