python练习项目: 将图片右上角加上红色的数字,类似于微信未读信息数量那种提示效果

一、安装绘制图片的相关仓库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)
三、效果

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/myydebk666/article/details/84593418
今日推荐