Use the composite tool from ImageMagick to add custom watermarks to your images.
Copy protection, particularly digital copy protection, is a topic that has been discussed more and more over recent years. In the case of protecting graphical content, often what you want the most is simply to prevent people from taking your images and using them as their own. To protect against redistribution, many online image galleries have taken to adding watermarks—logos or text that identify the owners of the content—to their images (see Figure 1-1). The watermark labels the owner of the content kind of like your mom writing your name on your underwear before you went to camp. If someone decides to take your image and host it on their site, they must cut out your watermark and leave an easily identifiable hole in the image. This hack will describe how to use the ImageMagick tool composite to add your own custom watermark to your image gallery.
Before you watermark your images, you must first create the image you will use as a watermark. How the watermark is designed is mostly a matter of taste, but there are a few conventions you can use to make a good watermark:
Your watermark should have relatively small dimensions as compared to the image you will watermark. This might seem to go without saying, but if a watermark is too big, not only will it prohibit people from copying your image, it will also prohibiting people from seeing your image. Try your watermark on a couple of representative images to see just how much of the image it obscures.
A watermark should have a transparent background. This convention follows the thinking of the previous one. The goal is to display the watermark but not obscure the image, so a transparent background ensures your logo will be seen but the large borders around the watermark won’t.
Consider a monochromatic watermark. It might be tempting to create a colorful and fancy watermark for your images, but the watermark will be combined with images of various colors, so a colorful watermark may clash with some of your images.
Once your watermark has been created, the next step is to decide the ideal placement on your images. This can be tough. After all, images come in lots of shapes and sizes, and—again—you don’t want to obscure a crucial part of the image. A common convention is to place watermarks near the bottom right-hand corner of an image. You could certainly place a watermark in the center of your image, but you run the chance of obscuring the part of the image people most want to see.
composite has a number of advanced options, but
for watermarking there are only a couple you absolutely need: -gravity
and -watermark
.The -gravity
option defines a number of locations
for a watermark. The supported locations are north, east, south, west,
northeast, southeast, southwest, northwest, and center. The -watermark
option takes as its argument a
percentage that defines how translucent to make the watermark (which
will determine how much your image will show through). To add a 35%
translucent watermark in the bottom right-hand corner of an image (Figure 1-1), type:
$ composite -watermark 35% -gravity southeast
watermark.gif image.jpg \ image-watermarked.jpg
In this example, watermark.gif
is the
watermark (with a transparent background),
image.jpg
is the image to watermark, and
image-watermarked.jpg
is the newly created
combination of the two. Once you have created the image, view it to make
sure that the transparency level is acceptable and the watermark is in
the correct location.
Tip
composite adds no padding to the watermark, so if you type -gravity southeast
, it puts the watermark
exactly in the bottom righthand corner of the image. If you want to
add padding, go into the watermark image itself and increase
the size of your canvas. Since the background of the watermark is
transparent, the larger canvas will act as padding.
Since this is a command-line program, it lends itself rather well to scripts. To add your watermark to all JPEG images in your current directory, type:
$ for i in *.jpg; do j=`echo $i | sed -e 's/\.jpg/-wm.jpg/'`; \ composite -watermark 35% -gravity southeast
watermark.gif
$i $j; done;
All the watermarked images will have -wm
added to the image name. To avoid accidentally renaming the files, you
might want to store all your watermarked files in a separate directory.
In that case, assuming the directory is called
~/watermarked, you would type:
$ for i in *.jpg; do composite -watermark 35% -gravity southeast \
watermark.gif
$i
~/watermarked/
$i; done;
Get Linux Multimedia Hacks now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.