Source

woocode / py / pil / add_watermark.py

Full commit
#!/usr/bin/env python
# -*- encoding:utf-8 -*-

import os
import Image, ImageEnhance


def batch(infolder, outfolder, watermark):
    mark = Image.open(watermark)
    for root, dirs, files in os.walk(infolder):
        for name in files:
            try:
                im = Image.open(os.path.join(root, name))
                if im.mode != 'RGBA':
                    im = im.convert('RGBA')
                layer = Image.new('RGBA', im.size, (0, 0, 0, 0))
                position = (im.size[0] - mark.size[0],
                            im.size[1] - mark.size[1])
                layer.paste(mark, position)
                Image.composite(layer, im, layer).save(os.path.join(outfolder, name))
            except Exception, e:
                print e

def test():
    batch('/home/yxy/Pictures', '/home/yxy/tmp', '/home/yxy/tmp/t.png')

if __name__ == '__main__':
    test()