Yesterday I bought Sony H50 point and shoot, how could I resist taking bulks of pictures! After filling up my PC with scores of pictures, my next task was watermark them IN BATCH. PIL striked my mind and the function in the end of the post followed.
While goolging, I have seen many tiny softwares doing same thing, but 1) none of them were capable with my Linux machine 2) and even on Windows, why should I make my registry dirty. So, here is the solution, only Python is needed.
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()
This is self explanatory. Function batch takes path of input folder, output folder and path to the watermark picture. Output folder must exist, it won't create it automatically.
Happy watermarking!

3 comments:
Just what I needed. Thanks!
Post a Comment