本题的链接来自https://github.com/Yixiaohan/show-me-the-code

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

头像




前期工作,安装PIL

使用python2.7 pip install PIL

使用python3.5 pip install pillow

PIL是图像处理库,但是只支持python2.7 ,pillow支持python3

pillow官方API:https://pillow.readthedocs.io/en/4.3.x/



代码

#coding:utf-8
from PIL import Image, ImageDraw, ImageFont, ImageColor
img = Image.open('QQ.jpg')
draw = ImageDraw.Draw(img) #创建Draw对象
Font = ImageFont.truetype('C:/Windows/Fonts/arial.ttf',size=40) #创建字体
width, height = img.size #获得图片的尺寸
draw.text((width-50,0),'12',font=Font,fill=(255,0,0))
img.save('out.jpg','jpeg')