In this article I’ll share a couple of ways to use ImageMagick to add a watermark to a photo. ImageMagick is a command line program, which is perfect for this job as that means it can scripted.
I use it on my photography website so I can upload original size photos, then the images can automatically be resized and have a watermark added. I won’t go into detail on the image resizing in this article, as I have a feeling the article will already be quite long. But maybe I will cover that aspect in a future article.
Overlay type watermark
First let’s take a look at adding a watermark that covers up part of the image, the standard style watermark that you’ll see on photos.
Designing your watermark
Assuming that you’re going to be automatically applying the watermark to photos, it is important to think about the tones being used in the watermark. A white watermark will show up well in a dark photo, but will be hidden in a bright photo. Further, what if the watermark is positioned over an area of a photo that contains both light and dark areas – only parts of the watermark would be visible.
For this reason, it makes sense to me to use a watermark with a background. Below you can see an example showing plain text watermarks in white and black versus a watermark incorporating a background. With the background the watermark is easily visible no matter what colour background the image has.
The style of your watermark is obviously completely up to you. I have gone for a grey background at 50% opacity with white text at 100% opacity on top.
You’ll probably want to create your watermark using a graphics editing program with a GUI. Though you could make something like this completely in ImageMagick if you desired.
If the watermark is intended to be applied to only a couple of different image sizes, then I suggest saving out versions of the watermark suited to those sizes of images.
ImageMagick can handle SVG files, which will scale to any size. But I would suggest using a raster format such as PNG instead. It ensures the watermark will be rendered correctly, and I suspect it should be faster.
Now let’s look at using ImageMagick to apply the watermark. If you’d like to follow along with the commands, you can download the files used: photo.jpg, watermark.png, watermark.svg, watermark-small.png
A watermark can be added to an image using the composite command:
composite -compose atop -geometry +75 -gravity southeast ./watermark.png ./photo.jpg ./photo-watermarked.jpg
If using an SVG image as the watermark you need to add
-background none to the command to stop a white background being applied to the SVG, and also add the height the SVG should be resized to in pixels e.g.
composite -compose atop -geometry x59+75 -gravity southeast -background none ./watermark.svg ./photo.jpg ./photo-watermarked.jpg
Of course, generally you’ll want to apply the watermark at the same time as resizing, and performing other operations on an image. Thankfully the same arguments will also work as part of a convert command, e.g.
convert \ -filter Lanczos \ ./photo.jpg \ -set option:filter:filter Lanczos \ -set option:filter:blur 0.8 \ -resize 500x500 \ -quality 80 \ ./watermark-small.png -gravity southeast -geometry +30 -composite \ ./photo-500px-watermarked.jpg
(If you’re working on windows you’ll need to replace the \ new line escape characters with a ^ instead. Or miss out the escape characters and put the command all on a single line.)
So, let’s look at the ImageMagick parameters involved with adding a watermark and what they mean:
Sets where in the image the watermark should be added. In these examples I’m using southeast, which puts the watermark in the bottom-right corner. You can use whatever direction you want, e.g. north for top centre. More info on the ImageMagick website: Command line options -gravity.
Geometry can be used to modify the size of the watermark being passed in, and also the positioning of the watermark (relative to the gravity placement). It is specified in the form width x height +/- horizontal offset +/- vertical offset.
If you have your watermark images already set to the correct size for usage, then you only need to specify the offset you want (if any). In my examples I’m specifying a positive horizontal offset, which pushes the watermark in from the right edge. More info on the ImageMagick website: Command line processing -geometry.
Lastly comes the -composite parameter, which tells ImageMagick to add the watermark image we’ve just specified to the image. More info on the ImageMagick website: Command line options -composite.
Border type watermark
For a border type watermark, the copyright notice or contact details are printed in a border round the image, rather than overlaying the image itself.
For this you will likely want a border that is larger in one direction (typically at the the bottom of the image), where the watermark can be added. ImageMagick doesn’t allow setting the border width as larger in only one direction (as far as I’m aware). But this can be worked around quite easily.
-bordercolor white -border 20x18 -gravity South -background white -splice 0x15
The above parameters can be added to a resize command to add the wanted border around the image. Let’s break it down:
Sets the colour of the border (who would’ve guessed?) You can read more about the different ways to specify color on the ImageMagick website: Command line options -fill.
Sets the border width on the x axis and the border height on the y axis. So in this example I am using a 20 pixel border left and right, and an 18 pixel border top and bottom. More info on the ImageMagick website: Command line options -border.
In this case we’re using gravity to control the placement of our border extension. Since I want the border to be larger at the bottom of the image, I’ve specified -gravity South.
Background specifies the background colour to be used for our border extension. You’ll want this to the be same colour as your border, in my case, white.
Is how the border is extended to the bottom of the image. Splice adds rows or columns to the image. Specifying a value of 0x15 means add no columns (which would affect the width) and add 15 pixels in height. Since we have set the gravity to South, the extra 15 pixels of height will be added to the bottom of the image. More info on the ImageMagick website: Command line options -splice.
Then the same arguments used to add an overlay style watermark can be used to add a watermark image to the border. You can actually add multiple watermarks, for example:
convert / -filter Lanczos / ./photo.jpg / -set option:filter:filter Lanczos / -set option:filter:blur 0.8 / -resize 500x500 / -quality 80 / -bordercolor white -border 20x18 -gravity South -background white -splice 0x15 / ./border-wm-left.png -gravity southwest -geometry +29+11 -composite / ./border-wm-right.png -gravity southeast -geometry +29+5 -composite / ./photo-500px-bordered.jpg
Then to automatically apply the watermark to images, it’s just a case of incorporating the relevant ImageMagick parameters as part of your image processing script. As I said earlier, you’ll probably want to be doing other things to your images at the same time, such as resizing, sharpening, and converting to sRGB for web usage. If I get time, I’ll cover these in a future article.
However, since I did title this article
How to automatically watermark or batch watermark photos using ImageMagick, I guess I should show something about how to actually batch add a watermark.
@echo off FOR %%i IN (./\*.jpg ./\*.jpeg) DO ( IF NOT EXIST ./\watermarked mkdir watermarked convert ^ -filter Lanczos ^ "%%i" ^ -set option:filter:filter Lanczos ^ -set option:filter:blur 0.8 ^ -resize 500x500 ^ -quality 80 ^ ../watermark-small.png -gravity southeast -geometry +30 -composite ^ "./watermarked/%%~nxi" )
Linux / Mac
for i in ./*.jpg ./*.jpeg do if [ ! -d "./watermarked" ]; then mkdir "./watermarked"; fi filename=$(basename "$i") ~/apps/ImageMagick/bin/convert \ -filter Lanczos \ "$i" \ -set option:filter:filter Lanczos \ -set option:filter:blur 0.8 \ -resize 500x500 \ -quality 80 \ ../watermark-small.png -gravity SouthEast -geometry +30 -composite \ "./watermarked/$filename" done
The above script will take all .jpg or .jpeg files in the current directory, resize to 500px on the longest edge, and add an overlay style watermark to the bottom right corner, indented 30px from the right edge. The watermarked images are then saved in a directory called ‘watermarked’.
You would need to save the script as watermark.bat (windows) or watermark.sh and make it executable (linux and mac). Then place the file in the directory with the images you want to watermark before running it. The script assumes that the watermark file is named watermark-small.png and placed in the parent folder.
Obviously I don’t recommend you use the above script as-is. But if you’re not familiar with shell scripting, it should at least give you a starting point you can work from, and modify to your own needs.