python3给图片添加水印

#!/usr/local/bin/python3
#coding=gbk

import os, io, sys, re, time, json, random
from PIL import Image, ImageEnhance, ImageFilter

def watermark(img_source, img_water, img_new, offset_x, offset_y):
    try:
        im = Image.open(img_source)
        wm = Image.open(img_water)
        layer = Image.new('RGBA', im.size, (0,0,0,0))
        layer.paste(wm, (im.size[0] - offset_x, im.size[1] - offset_y))
        newIm = Image.composite(layer, im, layer)
        newIm.save(img_new)

    except Exception as e:
        print(">>>>>>>>>>> WaterMark EXCEPTION:  " + str(e))
        return False
    else:
        return True

def main():
    watermark("original.jpg", "watermark.png", "afterwater.jpg", 250, 50)

if __name__ == '__main__':
    main()

 

原图:

 

水印:

 

添加水印后:

posted on 2013-07-23 17:06  岚之山  阅读(482)  评论(0编辑  收藏  举报

导航