Use Imagemagick to add watermark to images

There are obviously many different methods to create watermark for an image. However, in this article, I will show how to do the task without using any GUI application. Everything will totally be done in the terminal with the help of imagemagick.

I use one of my favorite images, the pic of Linus Torvalds raising his middle finger, as the example. I will add my watermark "linuxandlife.com" into the bottom right corner of this pic:


The method is very simple here. I will create a watermark image with a transparent background and the text "linuxandlife.com" at the bottom right corner. After that, I will combine 2 images together to have a watermarked image eventually. And all these steps will be done with only some commands in the terminal.

First, I will have to check the dimension of the Linus image. The command you can use is:
identify image-file

You will have an output like this:
fu.jpg JPEG 500x404 500x404+0+0 8-bit DirectClass 133KB 0.000u 0:00.000

 500x404 is the dimension of the image fu.jpg

Next, to create an image with a transparent background and the wartermark text in the corner, the command you can use is:
convert -size 500x404 xc:transparent -font Courier-bold -pointsize 25 -fill SlateBlue1 -draw "text 230,375 'linuxandlife.com'" output.png

What this command means:
-size 500x404 xc:transparent : to create a 500x404 image with a transparent background.
-font Courier-bold : font of the text
-pointsize 25 : size of the text
-fill SlateBlue1 : color of the text, you can check all the color codes for imagemagick here
-draw "text 230,275 'linuxandlife.com' : draw the text 'linuxandlife.com' at the 230:275 position

You will have the output like this:



The final step is to combine the two images. The command to do the task is:
composite -dissolve 50% -quality 100 lnl.png fu.jpg output.jpg

In this command:
composite : to combine two images
-dissolve 50% : to make the overlay image (lnl.png) 50% transparent
-quality 100 : keep the quality at 100%
lnl.png : the image with the text
fu.jpg : the Linus pic
output.jpg : the output image

And here is the result:


linux tips and tricks