Being a novice photographer, I usually have many pictures - sometimes in hundreds. While posting on Flickr or other online sources, it is very important to put watermark on all of them, like signature or email. This is a small self explanatory snippet which will place watermark image on right bottom corner. You just provide source folder and it will scan all photos in that folder (not sub folders) and will produce watermarked photos in the destination folder. It worked for me with jpg, it should work for all those formats which PIL can work with.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import Image, ImageEnhance, os
from os.path import join
def test():
batch("/media/disk/pics", "/home/hasanat/outputfolder/", "/home/hasanat/watermark.png")
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(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( join(outfolder, name))
except Exception, (msg):
print msg
if __name__ == '__main__':
test()
|