借鉴了教程:
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 = ''
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)
原图
效果图
哈哈好喜欢樱桃小丸子。