原GitHub地址:https://github.com/Yixiaohan/show-me-the-code
題目:將你的 QQ 頭像(或者微博頭像)右上角加上紅色的數字,類似于微信未讀資訊數量那種提示效果,
類似于圖中效果:
代碼:
from PIL import Image, ImageDraw, ImageFont
img = Image.open('0001.jpg') # 打開圖片
draw = ImageDraw.Draw(img) # 創建一個可用于繪制給定影像的物件
myfont = ImageFont.truetype('C:/windows/fonts/Arial.ttf', size=30) # 設定字體及字體大小
fillcolor = "#ff0000" # 設定填充顏色
draw.text((95,10),'8', font=myfont, fill=fillcolor) #將數字8畫在圖片的右上角,因為我的圖片比較小,所以我將數字的位置設定為(95,10)
img.save('result.jpg','jpeg') #保存畫好的圖片
效果如下:
原圖:
輸出:
附一個Pillow庫的檔案:https://pillow.readthedocs.io/en/3.1.x/index.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/194148.html
標籤:Python
