借鑒了教程:
http://yxnt.github.io/2016/05/15/Pillow-Python3.5/
完成作業如下:
后來學着寫給自己的圖片加了水印。
from PIL import Image,ImageDraw,ImageFont FileName = 'E:\savepicture\yuantu.jpg' OutName = 'E:\savepicture\shuiyin.jpg' BlogName = '小旋子的代碼自習室' BlogSite = 'http://www.cnblogs.com/pheobe/' def Add_shuiyin(FileName,OutName): with Image.open(FileName).convert('RGBA') as im: shuiyin = Image.new(im.mode,im.size) d = ImageDraw.Draw(shuiyin) Font1 =ImageFont.truetype( 'simfang.ttf',size=30) Font2 =ImageFont.truetype( 'simfang.ttf',size=20) d.text((100,300),BlogName,fill=(0,0,0),font=Font1) d.text((90,330),BlogSite,fill=(0,0,0),font=Font2) out = Image.alpha_composite(im,shuiyin) out.save(OutName) out.show() Add_shuiyin(FileName,OutName)
原圖
效果圖
哈哈好喜歡櫻桃小丸子。