Python practice questions (1)

1. Add a red number to the upper right corner of your QQ avatar (or Weibo avatar), which is similar to the prompt effect of the number of unread messages on WeChat. Similar to the effect in the picture

 

Implementation code:

# -*- coding: utf-8 -*-
"""
-------------------------------------------------
   File Name:     TEST
   Description :
   Author :       Administrator
   date:          2018-05-09
-------------------------------------------------
   Change Activity:
                   2018-05-09:
-------------------------------------------------
"""
__author__ = 'Administrator'

from PIL import Image, ImageFont, ImageDraw, ImageColor


def add_num(image, text):
    font = ImageFont.truetype( " arial.ttf " , 50)   #Set the font 
    fontcolor = ImageColor.colormap.get( ' yellow ' )   #Set the font color 
    draw = ImageDraw.Draw(image) #Add   the word to the picture 
    width, height = image.size #The   size of the newly generated image 
    draw.text((width-50, 30), text, font=font, fill= fontcolor)
    image.save( " E:\\IMAGE3.jpg " )   #Image save


if __name__ == '__main__':
    image = Image.open( ' E:\\myblog1\S1.jpg ' )   #Image position 
    text = ' 2 '   #Added words 
    add_num(image, text)

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326371342&siteId=291194637