Using Image Magick to create watermarks.
23 Jan 2021I wanted to add some small text to images, and got on this kick of using ImageMagick create small watermarks on a directory of images. Here’s the steps to do so, adapted from these docs.
First, we need to create a watermark image, and will need to select a font. What are all of the ImageMagick fonts? Good question. Calling
convert -list font > fonts.txt
in the command line will give you output of the fonts for ImageMagick. I chose NewCenturySchlbk-Italic
and I want the text to be A. Tabb
. Ok.
Once the font is selected, we’re going to generate the font with some black, white, and transparent fill sections, as below and Figure 1.
convert -size 320x100 xc:transparent -font NewCenturySchlbk-Italic -pointsize 72 \
-fill black -annotate +24+64 'A. Tabb' \
-fill white -annotate +26+66 'A. Tabb' \
-fill transparent -annotate +25+65 'A. Tabb' \
trans_stamp.png
Figure 1. trans_stamp.png.
This image is a little funky and not quite right, we are next going to create a mask. From the docs,
A “composite” mask image is a grey-scale image, pure black for parts that will be transparent, and pure white for any parts that you want completely visible (opaque). Any grey shades will be draw as semi-transparent, merging into the background colors underneath.
Note: we use .jpg
as the output because there are no transparency/ alpha channels in .jpg
, and all of the transparency information gets flattened by using that format. Did I try it another way? Why yes ….
convert -size 320x100 xc:black -font NewCenturySchlbk-Italic -pointsize 72 \
-fill white -annotate +24+64 'A. Tabb' \
-fill white -annotate +26+66 'A. Tabb' \
-fill black -annotate +25+65 'A. Tabb' \
mask_mask.jpg
Figure 2. mask_mask.jpg
.
Then, to merge these two images together so we only need one to create a watermark,
composite -compose CopyOpacity mask_mask.jpg trans_stamp.png \
trans_stamp3.png
Figure 3. trans_stamp3.png
.
Ok, the transparent watermark is set up. Now, to use it on a real image, this time, placing a small watermark in the lower right corner.
composite trans_stamp3.png image0.JPG -gravity southeast -geometry +10+10 output0.JPG
Figure 3. output0.JPG
.
Supposing you want to apply watermarks to a whole directory at a time, I use mogrify and “image Over” instead of the composite function. The “0,0 320,100” are the scaling of the trans_stamp3.png
image (I haven’t found the docs on this yet, to be updated later). Since our image has a 3.2:1 aspect ratio, if you want it larger (or smaller), adjust accordingly.
mogrify -gravity southeast -geometry +10+10 -draw "image Over 0,0 320,100 'trans_stamp3.png'" dir/*.JPG
Will give you exactly as the composite command did, applied to a directory of images. Whereas, changing the gravity option and scaling of trans_stamp3.png
by 3 as below,
mogrify -gravity center -geometry +0+0 -draw "image Over 0,0 960,300 'trans_stamp3.png'" dir/*.JPG
Figure 3. Example using a watermark in the center of the image.
I have another mogrify example in a TIL post.
There’s another post on this topic, A custom program to watermark images using Image Magick.