【Python學習筆記】-APP圖標顯示未讀消息數目
阿新 • • 發佈:2017-06-11
運行 讀取 lin 簡單實現 ets fcm 筆記 python語言 lte
以小米手機系統為例,當安裝的某個APP有未讀消息時,就會在該APP圖標的右上角顯示未讀消息的數目。本文主要解說怎樣用Python語言實現圖標顯示未讀消息的數目。首先,還是要用到Python中PIL庫,關於Linux下怎樣安裝PIL庫,請大家參考這篇博客:http://blog.csdn.net/kevin_zhai/article/details/47720721,裏面有具體的安裝過程。實現的原理非常easy,直接用Image讀取原始圖標。然後將未讀消息的數目插入到圖標的右上角就可以。
腳本代碼例如以下:
#coding=utf-8 import random import string import sys import math from PIL import Image,ImageDraw,ImageFont,ImageFilter #字體的位置,不同版本號的系統會有不同 font_path = "/usr/share/X11/fonts/Type1/c0611bt_.pfb" def gene_message_num(pic_path,message_num,fontcolor): ‘‘‘ @pic_path:原始頭像圖片的位置 @message_num:未讀消息的數目 @fontcolor:顯示消息數目的字體顏色 ‘‘‘ 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) #填充數字,位置在右上角 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)
結果展示:原始圖標和腳本運行和的圖標見下圖,因為僅僅是簡單實現,結果並非非常美觀。
【Python學習筆記】-APP圖標顯示未讀消息數目