Batch watermarking images in Linux

Screenshot from 2013-12-09 16:23:41I’ve been toying with the idea of watermarking my images on my original blog www.simplysimple.info for quite some time now. Evert time I tried over the last 3 to 4 years I’ve always failed. I’ve asked my designer friends and photographer friends a few times in the past and they have given me valid solutions, but processing about 1300 files individually and remembering to put them back in the same folder would have driven me mad. I needed batch processing. To add to my woes, WordPress converts every image I upload into 3 images -a thumbnail a medium sized image and a full sized image.

 Yesterday, I went into do or die mode, as I had been observing for some time on my web stats software Piwik, that people had been stealing images from my blog for which I had put in countless number of man hours, spent money, interviewed people and were responsible for several grey hairs on my head. Suddenly, out of the blue, the basics of watermarking just came to me -maybe from some divine source. This was what I needed.

  • A semi transparent image either a logo or text (or both) with its background deleted. (PNG format)
  • Add a layer and add the watermark to the original image and stretch/position it as required

Batch processing however is a compromise. you can’t predict where your logo will get stamped, so I chose to have my watermark diagonally on the images. It gets worse when the images are of different sizes with the whole watermark not visible on small thumbnail images, whereas on large images the text would be as small as subscript. I felt the compromises were good enough for me as its sole purpose was to dissuade content thieves.

I tried to Wine Irfanview on Ubuntu 13.10 (Saucy Salamander) but for some reason it wouldn’t even install. Then I tried a php script with some rewrites in my .htaccess file to apply watermarks on the fly, but I gave up easily as I wanted my files to be safe even on another webhost.

 Finally my solution came in the form of Tuxtweaks.com And the script shown below is from their website with some of the values tweaked for my requirements. Thanks guys!  Scroll down below the code to find out how I made a watermark for simplywired.com.


#!/bin/bash

###########################################
# NAME: wn-ow
# AUTHOR: Linerd (http://tuxtweaks.com), Copyright 2009
# LICENSE: Creative Commons Attribution – Share Alike 3.0 http://creativecommons.org/licenses/by-sa/3.0/
# You are free to use and/or modify this script. If you choose to distribute this script, with or
# without changes, you must attribute credit to the author listed above.
# REQUIRES: ImageMagick, coreutils
# VERSION: 1.0
# DESCRIPTION: A script to add a watermark and overwrite all images in a directory.
#
# This script will watermark all of the images in a directory. Warning! This will overwrite the files.
###########################################

# Initialize variables
WM=$HOME/Pictures/watermarks/watermark-siwd.png # This is the path to your watermark image
SCALE=60 # This sets the scale % of your watermark image

# Warning
echo -e “This will overwrite all of the images in this directory.”\\n”Are you shure want to continue? {Y/n}”
read REPLY

if
[ "$REPLY" != "n" ] && [ "$REPLY" != "N" ]
then
file -i * | grep image | awk -F':’ ‘{ print $1 }’ | while read IMAGE
do
echo Watermarking $IMAGE
composite -dissolve 60% -gravity center -quality 100 \( $WM -resize $SCALE% \) “$IMAGE” “$IMAGE”
done
else
echo exiting
exit 0
fi

exit 0

Okay, here we go! Fire up The Gimp first, if you don’t have it, install it from the software repository. You will also need Imagemagick. Make sure you have both of these installed.

Create a new image by Clicking on File > New . I retained the image dimensions shown by default

Create a new image by Clicking on File > New . I retained the image dimensions shown by default

 

Right click the new image and choose... Layer > Transparency > Add Alpha channel

Right click the new image and choose…
Layer > Transparency > Add Alpha channel

 

From the file menu, click on Layer > Delete layer

From the file menu, click on Layer > Delete layer

 

Screenshot from 2013-12-10 10:04:57

Click on the tool for Adding text

 

Now type in your text and adjust the font, size and color according to your preferences. I prefer to fill the frame

Now type in your text and adjust the font, size and color according to your preferences. I prefer to fill the frame with my text.

 

Next select the Rotate tool

Next select the Rotate tool

 

Click on the top left drag handle of the text box and rotate it to the desired angle. In my case I wanted to position it diagonally.

Click on the top left drag handle of the text box and rotate it to the desired angle. In my case I wanted to position it diagonally.

 

Now click on File > Export To and save it as a .png file

Now click on File > Export To and save it as a .png file

 

  • Move the watermark file to a folder on your computer, in may case ~/Pictures/watermarks/
  • Next create a bin folder inside your home folder (I already had one)
  • Create a new empty document by right clicking inside the bin folder
  • Open the file with the text editor and paste the whole code block in green (including the red above)
  • Now change the red text shown above to reflect your own folder where you saved your watermark. In my case ~/Pictures/watermarks/watermark-siwd.png
  • Save the file after tweaking the other settings in red, I’ve retained/changed settings to what worked for me.
  • Now make the file executable by either chmod +x filename from the terminal, or right click the file, Properties > Permissions and select the check box for ‘Allow executing this file as a program’

Finally I created a folder called convert to drop in pictures which needed to be watermarked.

This is how the watermarking is done.

  • Copy all the files you require into your conversion folder e.g ~/Pictures/convert 
  • Open a terminal window by pressing Control +T
  • change to your convert folder e.g cd ~/Pictures/convert
  • run the script which you saved. In my case ~/bin/wmark . I saved the script as wmark
  • Press y and all the files in your convert folder will be watermarked and overwritten.

Screenshot from 2013-12-09 16:23:41

Thanks Tuxtweaks.com for this splendid script

This post has been read 712 times :-)

Enjoyed this post? Share it!

 

Leave a comment

Your email address will not be published. Required fields are marked *


four × 9 =