Batch processing – Resizing and embedding watermarks into images using ImageMagick for Windows and Linux

By Christopher, March 19, 2017

As a response to a recent question I came across, I want to share a simple bash script for embedding a watermark image/logo and resizing multiple images at the same time for online publishing.

In order to use the script below you need to install imagemagick and if you are using Windows you either have to install the cygwin environment or you would have to rewrite this simple script as a bat script.

Cygwin: https://cygwin.com/install.html

Imagemagick: https://www.imagemagick.org/script/index.php
In most Linux systems you can just install it using your distros package manager, for instance

1
sudo apt install imagemagick

Your watermark image/logo should be a PNG file due to the transparency support of PNG.

Save the script below to watermark.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/bin/bash
# Author: Christopher Hrabia - www.ceh-photo.de

#adjust your output folder for watermarked images
OUTPUT_DIR='./watermarked/'

#adjust the target length in px of the larger image side, this preserves the aspect ratio
MAX_WIDTH=1600
MAX_HEIGHT=1600

#Determine the location of this script
CURRENT_SCRIPT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )

#adjust for your logo, path is determined relative to the script location
WATERMARK="$CURRENT_SCRIPT_DIR/your_logo.png"

#create the output directory if it does not exist
mkdir -p $OUTPUT_DIR

#iterate over all images forwarded as argument to this script
for pic in "$@"; do
    echo "Processing: $pic"
    #resize the image first to simplify image positioning
    convert "$pic" -resize $MAX_WIDTH"x>" -resize "x$MAX_HEIGHT>" "$OUTPUT_DIR$pic"
    #adjust parameters here for other watermark positions
    #current configuration is putting the watermark in the right bottom corner of the image
    composite -dissolve 30% -gravity SouthEast -geometry +25+5 "$WATERMARK" "$OUTPUT_DIR$pic" "$OUTPUT_DIR$pic"
done

Script usage:

1
2
3
cd PATH/TO/YOUR/IMAGE_FOLDER
#either call the script with an absolute path or add it to your PATH
watermark.sh *.jpg

The watermarked and resized pictures will be stored in the subdirectory “watermarked”.

What do you think?

You must be logged in to post a comment.