Batch Watermark Images in Linux

August 16, 2009 by
Filed under: bash, HowTo, linux, web development 

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:

chmod +x ~/bin/wm-ow

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.

Affiliate Link
#!/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:

chmod +x ~/bin/wm-new

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.

Comments

16 Responses to “Batch Watermark Images in Linux”

  1. Martin says:

    does'nt work for me. regardless of which path I use for the watermark graphic, I get an error when running the script that it cant open my watermark.png , no file or directory. i have my file in home/myname/Pictures/watermark.png and this is the path I have entered in the script.

  2. […] 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 […]

  3. Damian says:

    Nice article! May be you shuld use basename instead of pipe and cut but I'll give a try.

    Thanks.

    • Linerd says:

      basename requires you to provide the extension you want to strip, so you can't make it generic for all image types. Perhaps a loop could be written to figure out the correct extension, but it seems like it would be more work than my current code.

      On the other hand, my script assumes that the only '.' in the file name is between the name and the extension, so it's not perfect either.

  4. [...] exit 0 Note : This is an script by Linerd (http://tuxtweaks.com) you can view the original script here 3. Now in line 13 of the above script enter the path of your watermark image file such as your [...]

  5. Surfrock66 says:

    I'm having some issues; i'm completely new to Bash scripts. I'm getting an error:

    shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory

    Any thoughts?

    • Linerd says:

      Surfrock66 - did you perhaps have several terminals open to different directories and delete the working directory of one of the other terminals? You might get this message if you try to run the script from a directory that no longer exists. A google search on the error suggests some possible causes.

      I don't think the error has anything to do with the program itself.

  6. prasad says:

    Hi,

    I found a Webapplication that does batch watermarking (upto 20 images) and it has lot of options to customize the watermark.

    http://watermark-images.com

    Prasad.

  7. Kim Briggs says:

    "@ Kim – Interesting. I haven’t gotten into perl yet, so I don’t quite follow everything that’s going on. I’m guessing that chomp is a perl specific command?"

    chomp gets rid of the "new line" character that is included when you hit the "Enter/Return" button after submitting data. In this case, the year. The "convert" command is a ImageMagick command (man imagemagick for details).

  8. Jose_X says:

    I would change the first script to run only after Y/y. This way if the person types "stop" or "help" or "no" or "" etc, they won't have to worry.

    BTW, you can avoid being there to type "y" by doing: yes | thescript

  9. Duane Dier says:

    Nice work!

    I wrote a similar script that copies

    http://duane-pwns.blogspot.com/2008/09/batch-watermarking-photos-in-bash.html

    Yours is more concise, but mine was written for the specific purpose of creating a directory of smaller, watermarked images within the current directory. Let me know what you think!

    • Linerd says:

      @ Duane - Nice script. It looks like it could be done a little more efficiently. I like the ls | grep -ic jpg part. That's a neat trick to count the files. I like your watermark image too. I'll have to try making one like that in GIMP.

      The composite command should work fine if you just overwrite the file:
      composite -gravity SouthEast $watermark "$i" "$i"
      Then you can skip the rm command to clean up.

      @ Kim - Interesting. I haven't gotten into perl yet, so I don't quite follow everything that's going on. I'm guessing that chomp is a perl specific command?

      @ both - I guess what sets my script apart is the handling of multiple image file types. There's a lot going on in file -i * | grep image | awk -F':' '{ print $1 }' | while read IMAGE.
      file -i * - lists the files and shows file MIME type.
      grep image - filters the list for image files
      awk -F':' '{ print $1 }' - cuts the file name from the list item (everything before the ":" )
      while read IMAGE - takes the file name output from awk and stores it in $IMAGE

      I tested on a directory with .gif, .jpg, .png, .tiff files. File names with spaces work.

  10. Story added...

    This story has been submitted to fsdaily.com! If you think this story should be read by the free software community, come vote it up and discuss it here:

    http://www.fsdaily.com/Beginner/Batch_Watermark_Images_in_Linux...

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>