Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash -x
- # +-----------------------------------------------------------------------------------------+
- # | This script watermarks pictures in a directory |
- # | It keeps the original and adds a watermarked pic in the same directory |
- # | |
- # | Aug 2011 flip hess [email protected] |
- # +-----------------------------------------------------------------------------------------+
- #############################################################################################
- # Global variables:
- #############################################################################################
- PATH='/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin'
- SCRIPT_PATH="${0}"
- TOTARGS="${#}"
- ARG1="${1}"
- ARG2="${2}"
- TIME_STAMP="$( date '+%Y%m%d')"
- WATERMARK="/media/bigdisk/pictures/.watermark.png"
- #############################################################################################
- # Functions:
- #############################################################################################
- ##################################
- # exit function
- ##################################
- function die()
- {
- echo 'Watermark error!'
- echo "${1}"
- exit 1
- }
- ##################################
- # watermark pic
- ##################################
- function fPic()
- {
- # check arguments
- [ ${#} = 1 ] || return 0
- { [ -f "${1}" ] && fConvert "${1}"; } || die "Failed to convert picture"
- echo "Picture ${1} successfully converted!"
- return 0
- }
- ##################################
- # watermark dir
- ##################################
- function fDir()
- {
- # set var
- SOURCE="${1}"
- # check for argument
- [ ${#} = 1 ] || { echo "Too much variables! bark bark!"; return 1; }
- # check for empty source var
- [ -n ${SOURCE} ] || { echo "empty var.... thiz iz wonky!"; return 1; }
- # check if dir exitst
- [ -d ${SOURCE} ] || { echo "Source dir ${SOURCE} not found!"; return 1; }
- # if recursive; find pictures
- if [ ${RECURSIVE} = 1 ]; then
- function fCommand() { find ${1} -type f | grep -E '(jpg|jpeg|JPG|JPEG)' | grep -v 'marked_' ; }
- TOT="$( fCommand ${SOURCE} | wc -l )" || { echo "empty var.... thiz iz wonky!"; return 1; }
- elif [ ${RECURSIVE} = 0 ]; then
- function fCommand() { find ${1} -maxdepth 1 -mindepth 1 -type f|grep -E '(jpg|jpeg|JPG|JPEG)' | grep -v 'marked_' ; }
- TOT="$( fCommand ${SOURCE} | wc -l )" || { echo "empty var.... thiz iz wonky!"; return 1; }
- fi
- # if pics found, then:
- if [ ${TOT} != 0 ]; then
- # notify
- echo -e "\
- Found a total of ${TOT} pictures in directory ${SOURCE}\n\
- \n\
- Are you sure you want to convert all these ${TOT} pictures to watermarked pictures?\n\
- \n\
- "
- else
- echo -e "\
- No pictures found in in directory ${SOURCE}\n\
- Now Exiting ${SCRIPT_PATH}!\n\
- "
- return 1
- fi
- # are you sure?
- read -p "Please enter YES/Yes/yes or jup if you want to start converting ---> " INPUT
- # if confirmed
- if [ ${INPUT} = 'y' ] || [ ${INPUT} = 'Y' ] || [ ${INPUT} = 'yes' ] || [ ${INPUT} = 'Yes' ] || [ ${INPUT} = 'YES' ] || [ ${INPUT} = 'jup' ]; then
- # set count
- COUNT='0'
- # notify
- echo "Converting ${TOT} pictures. All watermarked versions wil be saved in the same directory"
- # for all image files in directory
- for IMAGE in $( fCommand ${SOURCE} )
- do
- # convert picture
- fConvert ${IMAGE} || { COUNT=$(( ${COUNT} ++1 )) ; echo "Failed to convert ${IMAGE} to a watermarked picture"; continue; }
- done
- else
- # notify canceled!
- die "Watermarking of ${SOURCE} canceled by user!"
- fi
- # notify
- if [ ${COUNT} = 0 ]; then
- echo "All pictures converted successfully!"
- else
- echo "Not all pictures converted successfully We had an error with in total: ${COUNT} pictures"
- fi
- return 0
- }
- ##################################
- # The watermark function.
- ##################################
- function fConvert()
- {
- SOURCE="${1}"
- [ -n ${SOURCE} ] || die "Something went wonky! No source defined in fConvert function!"
- # check if Source exists and is an image:
- [ -f ${SOURCE} ] || { echo "Source file ${SOURCE} not found!"; return 1; }
- file ${SOURCE} | grep -q 'JPEG image data' || { echo "Source file ${SOURCE} is not an JPEG image!"; return 1; }
- # if path is absolute set containing dir as dest, else use current dir
- # set dest and file name
- if ( echo "${SOURCE}" | grep -q '\/') ; then
- DEST="$( echo "${SOURCE}" | sed -e 's/[^\/]\+$//' )" || { echo "Failed to determine destination of the watermarked image"; return 1; }
- FILE="$( basename "${SOURCE}" )" || { echo "Failed to determine filename of the watermarked image"; return 1; }
- else
- DEST="$( pwd )" || { echo "Failed to determine destination of the watermarked image"; return 1; }
- FILE="$( basename ${SOURCE} )" || { echo "Failed to determine filename of the watermarked image"; return 1; }
- fi
- # convert source to new file
- composite -gravity southeast -dissolve 100 "${WATERMARK}" "${SOURCE}" "${DEST}marked_${FILE}" || { echo "Failed to add watermarks from ${SOURCE} to ${DEST}/${FILE}_marked.jpg"; return 1; }
- return 0
- }
- ##################################
- # The main function.
- ##################################
- function fMain()
- {
- # check for arguments
- [ ${TOTARGS} = 2 ] || { fShowUsage; exit 1; }
- # Arbiter:
- case "${ARG1}" in
- pic)
- fPic "${ARG2}"
- ;;
- dir)
- RECURSIVE=0
- fDir "${ARG2}"
- ;;
- all)
- RECURSIVE=1
- fDir "${ARG2}"
- ;;
- help)
- fShowUsage
- ;;
- *)
- fShowUsage
- ;;
- esac
- return 0
- }
- ##################################
- # The help function.
- ##################################
- function fShowUsage()
- {
- echo -e "\n\
- Watermark injector script\n\
- \n\
- Usage : ${SCRIPT_PATH} [ pic <image> ] [ dir <Directory> ] [ all <Directory> ] [ help ]\n\
- \n\
- pic <Picture> Adds the watermark to one picture\n\
- \n\
- dir <Directory> Adds the watermark to all pictures in directory\n\
- \n\
- all <Directory> Recursively adds the watermark to all pictures in directory\n\
- \n\
- help Shows this help index\n\
- \n\
- \n\
- "
- return 0
- }
- #############################################################################################
- # Do the magic:
- #############################################################################################
- # check for composite:
- [ -x $( which composite ) ] || die "This script ${SCRIPT_PATH} depends on ImageMagick please install it by running \"sudo apt-get install imagemagick\""
- # start the main function
- fMain "${ARG1}" "${ARG2}"
- # Exit with previous return code:
- exit "${?}"
Advertisement
Add Comment
Please, Sign In to add comment