from PIL import Image, ImageDraw, ImageFont
def image_add_text(img_path, text, left, top, text_color=(255, 0, 0), text_size=13):
img = Image.open(img_path)
# 创建一个可以在给定图像上绘图的对象
draw = ImageDraw.Draw(img)
# 字体的格式 simsun.ttc -宋体
fontStyle = ImageFont.truetype("simsun.ttc", text_size, encoding="utf-8")
# 绘制文本
draw.text((left, top), text, text_color, font=fontStyle)
return img
f = open('发送内容.txt',encoding = "utf-8")
data =f.read()
img_path = 're.png'
im = image_add_text(img_path, data, 10, 10, text_color=(0, 0, 0), text_size=20)
im.show()
PIL库的学习一、把文本写入图片
最新推荐文章于 2023-11-23 16:23:27 发布