2

I have a large directory tree with many sub-folders and lots of images within each one. I found a script that applies watermark to all images within a directory, in-place, overwriting the original image. I'm looking for the best way to iterate trough all the folders and subfolders and run the "compose" command for each image on my linux server. Here is the original script I found

#!/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/public_html/image/catalog/logo-website/watermark.png  # This is the path to your watermark image
SCALE=100                          # 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 SouthEast -quality 100 \( $WM -resize $SCALE% \) "$IMAGE" "$IMAGE"
        done
else
    echo exiting
    exit 0
fi

exit 0

Should I use a combination of "find . -name *.jpg" or something else?

Roberto C
  • 121
  • 3
  • I'm trying to do it for *,JPG. I only have .JPG files. I just tried the below command, but it complains about invalid filenames, I think because the folders and sub-folders have names with spaces like "17 inches". `WM=$HOME/public_html/image/catalog/logo-website/watermark.png SCALE=100 for f in `find . -iname "*.jpg"` do composite -dissolve 40% -gravity SouthEast -quality 100 \( $WM -resize $SCALE% \) "$f" "$f" done exit 0` I get "composite: unable to open image `./17': No such file or directory @ error/blob.c/OpenBlob/2588." – Roberto C Oct 04 '15 at 22:53
  • 2
    You don't need that whole script to do that... `find . -iname '*.jpg' -exec echo composite -dissolve 40% -gravity SouthEast -quality 100 /path/to/watermark -resize 100 {} {} \;` Replace _/path/to/watermark_ and _100_ after _-resize_ with your actual values and if you're happy with the result remove the `echo` in front of `composite` – don_crissti Oct 04 '15 at 23:05
  • hah...yeah, your answer is much better @don_crissti you should put it in as the proper answer – seumasmac Oct 04 '15 at 23:12
  • For some reason it makes my images very small resolution and poor quality. I use this `find . -iname '*.jpg' -exec composite -dissolve 40% -gravity SouthEast -quality 100 /home/public_html/image/catalog/logo-website/watermark.png -resize 100% {} {} \;` What am I missing? – Roberto C Oct 04 '15 at 23:32

1 Answers1

1

The script already checks the directory for image files, but if you want to adjust it to go through all your directories and not prompt you for each one, you might as well just rewrite it. Something like:

#!/bin/bash

WM=$HOME/public_html/image/catalog/logo-website/watermark.png  # This is the path to your watermark image
SCALE=100                          # This sets the scale % of your watermark image
STARTDIR="/home/whatever/images"   # This is the directory to start in

for imagedir in $( find $STARTDIR -type d )
do
  echo "Running in $imagedir ..."
  cd $imagedir
  file -i * | grep image | awk -F':' '{ print $1 }' | while read IMAGE
  do
    echo "Watermarking $IMAGE"
    composite -dissolve 40% -gravity SouthEast -quality 100 \( $WM -resize $SCALE% \) "$IMAGE" "$IMAGE"
  done
done
seumasmac
  • 2,015
  • 13
  • 14
  • Can someone explain to me why if I run the command in a folder with under 1000 images it works ok, but running it in folder with more than 3000 images, it does nothing, no files are being touched and also no errors. It runs until I ctrc+c the execution. `find . -iname '*.jpg' -exec composite -dissolve 40% -gravity SouthEast -quality 100 /home/cauciucurixxl/public_html/image/catalog/logo-website/watermark.png {} {} \; ` – Roberto C Oct 06 '15 at 23:09
  • You're probably hitting the "maximum length of a command" limit. Try with `\+` on the end instead of `\;` – seumasmac Oct 06 '15 at 23:26
  • Eventually it ran, but it took a whooping 4 hours to complete. – Roberto C Oct 07 '15 at 16:42