Last active
December 15, 2015 15:19
-
-
Save lnsoso/5280538 to your computer and use it in GitHub Desktop.
create watermarks with Python PIL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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