题目:将你的 QQ 头像(或者微博头像)右上角加上红色的数字,类似于微信未读信息数量那种提示效果。
类似于图中效果:python练习——第0题 Python 第1张

代码:

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
from PIL import Image, ImageDraw, ImageFont

img = Image.open('0001.jpg') # 打开图片
draw = ImageDraw.Draw(img) # 创建一个可用于绘制给定图像的对象
myfont = ImageFont.truetype('C:/windows/fonts/Arial.ttf', size=30) # 设置字体及字体大小
fillcolor = "#ff0000" # 设置填充颜色
draw.text((95,10),'8', font=myfont, fill=fillcolor) #将数字8画在图片的右上角,因为我的图片比较小,所以我将数字的位置设置为(95,10)
img.save('result.jpg','jpeg') #保存画好的图片

效果如下:
原图:python练习——第0题 Python 第2张

输出:python练习——第0题 Python 第3张

附一个Pillow库的文档:https://pillow.readthedocs.io/en/3.1.x/index.html

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄