python PIL 操作圖片

第 0000 題:將你的 QQ 頭像(或者微博頭像)右上角加上紅色的數字,類似於微信未讀信息數量那種提示效果。 類似於圖中效果

頭像

https://github.com/Yixiaohan/show-me-the-code


首先要安裝PIL 庫,參考http://liam0205.me/2015/04/22/pil-tutorial-basic-usage/

sudo pip install Pillow


然後python 如下

from PIL import Image, ImageDraw
import sys

def DrawNotify(pic,text):
    image = Image.open(pic)
    imgdraw = ImageDraw.Draw(image)
    #should be size ,not size()
    width,height = image.size 
    #notice that --> w
    #           |
    #           V
    #           h
    #should []
    imgdraw.text([0.9*width,0.1*height],text,255) 
    del imgdraw
    #image.show()
    image.save(pic)
    
if __name__ == '__main__':
    #accept first and second arguments 
    DrawNotify(sys.argv[1],sys.argv[2])



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章