added several image extensions,
added deletion (commented out) or moving the original file to another directory, Rob thx
.heic doesn't work =)
#!/usr/bin/env python
# encoding: utf-8
import os
import argparse
import shutil
def main():
parser = argparse.ArgumentParser(description='Add watermarks to images in path')
parser.add_argument('--root', help='Root path for images', required=True, type=str)
parser.add_argument('--watermark', help='Path to watermark image', required=True, type=str)
parser.add_argument('--name', help='Name addition for watermark', default="-WM", type=str)
parser.add_argument('--extension', help='Image extensions to look for', default=[".jpg", ".jpeg", ".JPG", ".JPEG", ".heic", ".HEIC"], nargs='+', type=str)
parser.add_argument('--exclude', help='Path content to exclude', type=str)
args = parser.parse_args()
files_processed = 0
files_watermarked = 0
for dirName, subdirList, fileList in os.walk(args.root):
if args.exclude is not None and args.exclude in dirName:
continue
for fname in fileList:
files_processed += 1
print(' Processing %s' % os.path.join(dirName, fname))
if any(ext in fname.lower() for ext in args.extension) and args.watermark not in fname and args.name not in fname:
ext = '.'.join(os.path.basename(fname).split('.')[1:])
orig = os.path.join(dirName, fname)
new_name = os.path.join(dirName, '%s.%s' % (os.path.basename(fname).split('.')[0] + args.name, ext))
if not os.path.exists(new_name):
files_watermarked += 1
print(' Convert %s to %s' % (orig, new_name))
os.system('composite -dissolve 30%% -tile %s "%s" "%s"' % (args.watermark, orig, new_name))
#os.remove(orig) # del orig file or move next 3 steps
new_dir = os.path.join(args.root, dirName.replace(args.root, '/mnt/archive/'))
os.makedirs(new_dir, exist_ok=True)
shutil.move(orig, os.path.join(new_dir, os.path.basename(orig)))
print("Files Processed: %s" % "{:,}".format(files_processed))
print("Files Watermarked: %s" % "{:,}".format(files_watermarked))
if __name__ == '__main__':
main()