题目004:将你的 QQ 头像(或者微博头像)右上角加上红色的数字,类似于微信未读信息数量那种提示效果。 类似于图中效果
# -*- coding: UTF-8 -*-
from PIL import Image,ImageFont,ImageDraw
def add_num(in_file,num,out_file='result.jpg'):
# 打开图片,建立画布
im = Image.open(in_file)
draw = ImageDraw.ImageDraw (im)
fill_color = '#ff0000'#修改字体颜色
#fill_color = 'red'
width,height = im.size
font = ImageFont.truetype ('C:/Windows/fonts/Arial.ttf', 100) # 修改字体大小,这里是100
draw.text((width-200,10),str(num),fill=fill_color,font=font)#(width-200,10)可以修改字体的位置
im.save(out_file)
add_num('test.jpg',520)
效果如下: