#-*- coding:utf-8 -*-
importosfrom PIL importImage,ImageDraw,ImageFont#图片路径,水印,文件名
defwatermake_picture(imagefile,text,filename):
img=Image.open(imagefile)
(img_x,img_y)=img.size#字体样式,以及字体大小
fontsize=60fontpath='C:\\WINDOWS\\Fonts\\STXINGKA.TTF'#华文行楷
font=ImageFont.truetype(fontpath,fontsize)#添加字体的位置
font_x=img_x - int((img_x * 1.5) / 10)
font_y=img_y - int((img_y * 1.5) / 10)#字体颜色(以下两种都可以)
#fill = (0, 0, 0)
fill='green'img_draw=ImageDraw.Draw(img)
img_draw.text((font_x,font_y),text.decode('utf-8'),fill,font)#创建新的文件来存储图片
new_img_path='new images'
if notos.path.exists(new_img_path):
os.makedirs(new_img_path)
img.save(new_img_path+'/'+filename+'.jpg')if __name__=='__main__':#获取当前文件夹下的所有文件
files=os.listdir(os.getcwd())for filename infiles:if 'jpg'==filename.split('.')[-1].lower() or 'png'==filename.split('.')[-1].lower():
img=Image.open(filename)
img.show()#打开图片
text=raw_input('请输入该张图片要加入的水印:')
imgName=raw_input('该图片保存的名字为:')
watermake_picture(filename,text,imgName)print imgName+u'.jpg创建成功!'