In the case of Xiaomi's mobile phone system, when an app is installed with unread messages, the number of unread messages is displayed in the upper-right corner of the app icon. This article explains how to implement an icon to display the number of unread messages in the Python language. First of all, still need to use Python PiL library, about Linux how to install PIL library, please refer to this blog: http://blog.csdn.net/kevin_zhai/article/details/47720721, there are specific installation process. The principle of implementation is very easy to read the original icon directly with image. You can then insert the number of unread messages into the upper-right corner of the icon.
Script code such as the following:
#coding =utf-8import randomimport stringimport sysimport mathfrom PIL Import image,imagedraw,imagefont,imagefilter# Font location, different version number of the system will have different Font_path = "/USR/SHARE/X11/FONTS/TYPE1/C0611BT_.PFB" def gene_message_num (Pic_path,message_num, FontColor): " @pic_path: The position of the original avatar picture @message_num: Number of unread messages @fontcolor: The font color of the number of messages displayed " Image = Image.open (pic_path) size = image.size width,height = size #原始图片的宽和高 font = imagefont.truetype (font _path,30) #字体 draw = Imagedraw.draw (image) #创建画笔 text = str (message_num) font_width, font_height = Font.getsize (text) Draw.text ((width-font_width, 0), text, font=font,fill=fontcolor) #填充数字, position in upper right corner Image.Save (' 1.jpg ') #保存图片if __name__ = = "__main__": pic_path = "Qq.jpg" message_num = 4 fontcolor = ( 255,0,0) Gene_message_num (Pic_path,message_num,fontcolor)
Results show: The original icon and the script run and the icon see, because only simple implementation, the result is not very beautiful.
Python Learning Notes-app icon shows the number of unread messages