一、安装绘制图片的相关仓库PIL
廖雪峰的官方网站
二、代码
from PIL import Image, ImageDraw, ImageFont,ImageColor
def add_num(image,text):
#设置字体(字体样式,字体大小)
font = ImageFont.truetype("arial.ttf",250)
#设置字体颜色
fontcolor = ImageColor.colormap.get('red')
#ImageDraw模块的函数:Draw(image):创建一个可以在给定图像上绘图的对象
draw = ImageDraw.Draw(image)
#获取图片大小
width , height = image.size
#将文字加到图片上
draw.text((width-150,30),text,font=font,fill=fontcolor)
#特别要注意这个文件路径名\\head2.png这里有两个\
image.save("G:\pycharm_python\photo\\head2.png")
if __name__=='__main__':
image = Image.open('G:\pycharm_python\photo\\head.png')
#将到图片上的文本内容
text = "5"
add_num(image,text)