Skip to content

Instantly share code, notes, and snippets.

@lnsoso
Last active December 15, 2015 15:19
Show Gist options
  • Save lnsoso/5280538 to your computer and use it in GitHub Desktop.
Save lnsoso/5280538 to your computer and use it in GitHub Desktop.
create watermarks with Python PIL
import Image, ImageEnhance, os
from os.path import join
def test():
batch("./src", "./dst", "./logo/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]/2-mark.size[0]/2, im.size[1]/2-mark.size[1]/2)
layer.paste(mark, position)
Image.composite(layer, im, layer).save( join(outfolder, name))
except Exception, (msg):
print msg
if __name__ == '__main__':
test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment