python小程序-0001

来源:互联网 发布:酒店营业数据分析 编辑:程序博客网 时间:2024/06/08 17:00

第 1题: 在图片右上角加上红色的数字,类似于微信未读信息数量那种提示效果。

#!/usr/bin/env python3# -*- coding : utf-8 -*-from PIL import Imagefrom PIL import ImageDrawfrom PIL import ImageFontdef addnumtoImage(imagepath,num):    img    = Image.open(imagepath)    myfont = ImageFont.truetype("C:\Windows\Fonts\calibri.ttf",24)    width,height = img.size    draw   = ImageDraw.Draw(img)    draw.text((width * 0.9,width * 0.07),num,font = myfont,fill = (0xff,0,0))    img.save('result.jpg')   # img.show()if __name__ == '__main__':    image = input("Please enter filepath:")    addnum = input("please enter a num: ")    addnumtoImage(image,addnum)

效果如图:
这里写图片描述