How to add watermarks to images via command line - Hopefully using Irfanview
Asked Answered
R

3

32

I have done a bit of digging and i havn't been able to find any feasible way of adding watermarks to my 1000+ images automatically. Is this possible with irfanview?? What im looking for is just some basic transparent text overlaying across each image. Can this be done using command line? Is it possible to go one step further and add a logo watermark?

Can you recommend any other programs rather than irfanview to do this, if its not possible to do it in this program.

Rasp answered 4/4, 2011 at 0:19 Comment(0)
B
46

I recommend using ImageMagick, which is open source and quite standard for manipulating images on the command line.

Watermarking with an image is as simple as

composite -dissolve 30% -gravity south watermark.jpg input-file.jpg output-file.jpg

With text, it's a little more complicated but possible.

Using the above command as an example, a Bash command for doing this to all files in folder would be:

for pic in *.jpg; do
    composite -dissolve 30% -gravity south watermark.jpg $pic ${pic//.jpg}-marked.jpg
done

For more information about watermarking with ImageMagick, see ImageMagick v6 Examples.

Billiards answered 4/4, 2011 at 2:6 Comment(0)
M
3

Here's a quick python script based on the ImageMagik suggestion.

#!/usr/bin/env python
# encoding: utf-8

import os
import argparse

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="-watermark", type=str)
    parser.add_argument('--extension', help='Image extensions to look for', default=".jpg", 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
        #print('Walking directory: %s' % dirName)
        for fname in fileList:
            files_processed += 1
            #print('  Processing %s' % os.path.join(dirName, fname))
            if args.extension in fname 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%% -gravity SouthEast %s "%s" "%s"' % (args.watermark, orig, new_name))

    print("Files Processed: %s" % "{:,}".format(files_processed))
    print("Files Watermarked: %s" % "{:,}".format(files_watermarked))


if __name__ == '__main__':
    main()

Run it like this:

./add_watermarks.py --root . --watermark copyright.jpg --exclude marketplace

To create the watermark I just created the text in a Word document then did a screen shot of the small area of the text to end up with a copyright.jpg file.

Medicable answered 8/1, 2017 at 19:56 Comment(0)
C
0

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()
Christoffer answered 22/9, 2023 at 6:30 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.