Batch Watermark Images in Linux
I previously wrote up a post showing how to watermark images in Linux with ImageMagick. Without too much work you can write a script to do batch processing of your images. This way you can watermark a whole directory of images at once.
I've written two versions of this script. The first one I'll show overwrites all of the images in a directory with the watermarked images. This should be done with some degree of caution as there is no way to undo the changes unless you have the files backed up. Save the following script in your ~/bin/ directory and name it wm-ow.
#!/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/Documents/Watermark.png # This is the path to your watermark image
SCALE=50 # 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 40% -gravity center -quality 100 \( $WM -resize $SCALE% \) "$IMAGE" "$IMAGE"
done
else
echo exiting
exit 0
fi
exit 0
Make sure to make the file executable with:
The second version of this script will not overwrite your images. It will create a new file with _wm inserted into the file name. This is the version you should use if you're not sure about what settings to use in the script. Save the following in ~/bin/ as wm-new.
#!/bin/bash
###########################################
# NAME: wm-new
# 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 to all images in a directory.
# Original images will be retained and new watermarked images will be created.
#
###########################################
# Initialize variables
WM=$HOME/Documents/Watermark.png # This is the path to your watermark image
SCALE=50 # This sets the scale % of your watermark image
# Find all image files in current directory and watermark.
file -i * | grep image | awk -F':' '{ print $1 }' | while read IMAGE
do
echo Watermarking $IMAGE
NAME=`echo $IMAGE | cut -f1 -d.`
EXT=`echo $IMAGE | cut -f2 -d.`
composite -dissolve 40% -gravity center -quality 100 \( $WM -resize $SCALE% \) "$IMAGE" "${NAME}_wm.${EXT}"
done
exit 0
Make sure to make it executable with:
NOTE: In both of the above scripts line 17 defines the location of the file that you want to use as a watermark. Line 18 defines the scale of the watermark in terms of percent.
I'm thinking that these two could be combined into one. I could present an option for the user and use a case statement to choose which routine to run.
How To Watermark Images With ImageMagick in Linux
I recently discovered that one of my most popular posts had been poached by another blog with out providing credit - images and all. I decided that it would be best to watermark my images to make it more difficult to rip off my content.
This procedure could also be done easily in GIMP, but it takes time to open up each image and then import or create the watermark text. Instead I created my desired watermark text in GIMP and saved it as a separate file called Watermark.png. This file has a transparent background, so only the text will show up when it's overlaid on another image.
For my demo I'm going to overlay the watermark on an image of Tux, the Linux mascot.
To create the watermark I'm going to use the composite command from the ImageMagick package. The structure of my command assumes that both the watermark and target images are in the same directory.
composite -dissolve 50% -gravity center -quality 100 Watermark.png Tux.jpg Tuxwm.jpgThis creates a new image called Tuxwm.jpg.
A little explanation of the command we performed. composite is the command to combine two images. The dissolve 50% switch tells ImageMagick to make the overlay image 50% transparent. The gravity center switch tells it to center the overlay image on the base image.
As you can see above, my watermark image is too large compared to the image of Tux. I'll need to shrink it in order for the result to look how I want. Now, I could go into GIMP and resize my watermark image, but ImageMagick provides an easy way to do this on the fly. With a slight modification to the above command I can scale down the watermark before it's overlaid.
composite -dissolve 50% -gravity center -quality 100 \( Watermark.png -resize 50% \) Tux.jpg Tuxwm.jpgThat looks a little bit better. Note that these commands have created a new image called Tuxwm.jpg. You can make the command overwrite the original image if you just pass the same file name at the end of the command like this.
composite -dissolve 50% -gravity center -quality 100 \( Watermark.png -resize 50% \) Tux.jpg Tux.jpgThis may seem a bit complicated to type this command to watermark a single image, but the nice thing is that it can be automated in a script so that you can watermark a whole directory of images at the same time. You can see that post here.





