flipje

foto-watermarker

Aug 24th, 2011
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 7.24 KB | None | 0 0
  1. #!/bin/bash -x
  2. # +-----------------------------------------------------------------------------------------+
  3. # | This script watermarks pictures in a directory                                          |
  4. # | It keeps the original and adds a watermarked pic in the same directory                  |
  5. # |                                                                                         |
  6. # | Aug 2011 flip hess [email protected]                                                  |
  7. # +-----------------------------------------------------------------------------------------+
  8.  
  9.  
  10. #############################################################################################
  11. # Global variables:
  12. #############################################################################################
  13.  
  14.   PATH='/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin'
  15.   SCRIPT_PATH="${0}"
  16.   TOTARGS="${#}"
  17.   ARG1="${1}"
  18.   ARG2="${2}"
  19.  
  20.   TIME_STAMP="$( date '+%Y%m%d')"
  21.  
  22.   WATERMARK="/media/bigdisk/pictures/.watermark.png"
  23.  
  24. #############################################################################################
  25. # Functions:
  26. #############################################################################################
  27.  
  28.   ##################################
  29.   # exit function
  30.   ##################################
  31.   function die()
  32.   {
  33.     echo 'Watermark error!'
  34.     echo "${1}"
  35.     exit 1
  36.   }
  37.  
  38.   ##################################
  39.   # watermark pic
  40.   ##################################
  41.   function fPic()
  42.   {
  43.    # check arguments
  44.    [ ${#} = 1 ] || return 0
  45.    { [ -f "${1}" ] && fConvert "${1}"; } || die "Failed to convert picture"
  46.  
  47.    echo "Picture ${1} successfully converted!"
  48.    return 0
  49.  
  50.   }
  51.  
  52.   ##################################
  53.   # watermark dir
  54.   ##################################
  55.   function fDir()
  56.   {
  57.    # set var
  58.    SOURCE="${1}"
  59.  
  60.    # check for argument
  61.    [ ${#} = 1 ] || { echo "Too much variables! bark bark!"; return 1; }
  62.    # check for empty source var
  63.    [ -n ${SOURCE} ] || { echo "empty var.... thiz iz wonky!"; return 1; }
  64.    # check if dir exitst
  65.    [ -d ${SOURCE} ] || { echo "Source dir ${SOURCE} not found!"; return 1; }
  66.  
  67.    # if recursive; find pictures
  68.    if [ ${RECURSIVE} = 1 ]; then  
  69.      function fCommand() { find ${1} -type f | grep -E '(jpg|jpeg|JPG|JPEG)' | grep -v 'marked_' ; }
  70.      TOT="$( fCommand ${SOURCE} | wc -l )" || { echo "empty var.... thiz iz wonky!"; return 1; }
  71.    elif [ ${RECURSIVE} = 0 ]; then
  72.      function fCommand() { find ${1} -maxdepth 1 -mindepth 1 -type f|grep -E '(jpg|jpeg|JPG|JPEG)' | grep -v 'marked_' ; }
  73.      TOT="$( fCommand ${SOURCE} | wc -l )" || { echo "empty var.... thiz iz wonky!"; return 1; }
  74.    fi
  75.  
  76.    # if pics found, then:  
  77.    if [ ${TOT} != 0 ]; then
  78.      # notify
  79.      echo -e "\
  80.     Found a total of ${TOT} pictures in directory ${SOURCE}\n\
  81.     \n\
  82.     Are you sure you want to convert all these ${TOT} pictures to watermarked pictures?\n\
  83.     \n\
  84.     "
  85.    else
  86.      echo -e "\
  87.     No pictures found in in directory ${SOURCE}\n\
  88.     Now Exiting ${SCRIPT_PATH}!\n\
  89.     "
  90.      return 1
  91.    fi
  92.  
  93.    # are you sure?
  94.    read -p "Please enter YES/Yes/yes or jup if you want to start converting ---> " INPUT
  95.  
  96.    # if confirmed
  97.    if [ ${INPUT} = 'y' ] || [ ${INPUT} = 'Y' ] || [ ${INPUT} = 'yes' ] || [ ${INPUT} = 'Yes' ] || [ ${INPUT} = 'YES' ] || [ ${INPUT} = 'jup' ]; then
  98.      # set count
  99.      COUNT='0'
  100.      # notify
  101.      echo "Converting ${TOT} pictures. All watermarked versions wil be saved in the same directory"
  102.      # for all image files in directory
  103.      for IMAGE in $( fCommand ${SOURCE} )
  104.      do
  105.        # convert picture
  106.        fConvert ${IMAGE} || { COUNT=$(( ${COUNT} ++1 )) ; echo "Failed to convert ${IMAGE} to a watermarked picture"; continue; }
  107.      done
  108.    else
  109.      # notify canceled!
  110.      die "Watermarking of ${SOURCE} canceled by user!"
  111.    fi
  112.  
  113.    # notify
  114.    if [ ${COUNT} = 0 ]; then
  115.      echo "All pictures converted successfully!"
  116.    else
  117.      echo "Not all pictures converted successfully We had an error with in total: ${COUNT} pictures"
  118.    fi
  119.  
  120.    return 0
  121.  
  122.   }
  123.  
  124.  
  125.   ##################################
  126.   # The watermark function.
  127.   ##################################
  128.   function fConvert()
  129.   {
  130.     SOURCE="${1}"
  131.  
  132.     [ -n ${SOURCE} ] || die "Something went wonky! No source defined in fConvert function!"
  133.  
  134.     # check if Source exists and is an image:
  135.     [ -f ${SOURCE} ] || { echo "Source file ${SOURCE} not found!"; return 1; }
  136.     file ${SOURCE} | grep -q 'JPEG image data' || { echo "Source file ${SOURCE} is not an JPEG image!"; return 1; }
  137.    
  138.     # if path is absolute set containing dir as dest, else use current dir
  139.     # set dest and file name
  140.     if ( echo "${SOURCE}" | grep -q '\/') ; then
  141.       DEST="$( echo "${SOURCE}" | sed -e 's/[^\/]\+$//' )" || { echo "Failed to determine destination of the watermarked image"; return 1; }
  142.       FILE="$( basename "${SOURCE}" )" ||  { echo "Failed to determine filename of the watermarked image"; return 1; }
  143.     else
  144.       DEST="$( pwd )" ||  { echo "Failed to determine destination of the watermarked image"; return 1; }
  145.       FILE="$( basename ${SOURCE} )" || { echo "Failed to determine filename of the watermarked image"; return 1; }
  146.     fi
  147.  
  148.     # convert source to new file
  149.     composite -gravity southeast -dissolve 100 "${WATERMARK}" "${SOURCE}" "${DEST}marked_${FILE}" || { echo "Failed to add watermarks from ${SOURCE} to ${DEST}/${FILE}_marked.jpg"; return 1; }
  150.  
  151.     return 0
  152.  
  153.   }
  154.  
  155.  
  156.   ##################################
  157.   # The main function.
  158.   ##################################
  159.   function fMain()
  160.   {
  161.     # check for arguments
  162.     [ ${TOTARGS} = 2 ] || { fShowUsage; exit 1; }
  163.  
  164.     # Arbiter:
  165.     case "${ARG1}" in
  166.       pic)
  167.         fPic "${ARG2}"
  168.       ;;
  169.       dir)
  170.         RECURSIVE=0
  171.         fDir "${ARG2}"
  172.       ;;
  173.       all)
  174.         RECURSIVE=1
  175.         fDir "${ARG2}"
  176.       ;;
  177.       help)
  178.         fShowUsage
  179.       ;;
  180.       *)
  181.         fShowUsage
  182.       ;;
  183.     esac
  184.  
  185.     return 0
  186.  
  187.   }
  188.  
  189.  
  190.   ##################################
  191.   # The help function.
  192.   ##################################
  193.   function fShowUsage()
  194.   {
  195.     echo -e "\n\
  196.    Watermark injector script\n\
  197.    \n\
  198.    Usage : ${SCRIPT_PATH} [ pic <image> ]  [ dir <Directory> ] [ all <Directory> ] [ help ]\n\
  199.    \n\
  200.    pic <Picture>          Adds the watermark to one picture\n\
  201.    \n\
  202.    dir <Directory>        Adds the watermark to all pictures in directory\n\
  203.    \n\
  204.    all  <Directory>       Recursively adds the watermark to all pictures in directory\n\
  205.    \n\
  206.    help                   Shows this help index\n\
  207.    \n\
  208.    Flip Hess 2011 - [email protected]\n\
  209.    \n\
  210.    "
  211.  
  212.     return 0
  213.  
  214.   }
  215.  
  216. #############################################################################################
  217. # Do the magic:
  218. #############################################################################################
  219.  
  220.   # check for composite:
  221.   [ -x $( which composite ) ] || die "This script ${SCRIPT_PATH} depends on ImageMagick please install it by running \"sudo apt-get install imagemagick\""
  222.  
  223.   # start the main function
  224.   fMain "${ARG1}" "${ARG2}"
  225.  
  226.   # Exit with previous return code:
  227.   exit "${?}"
Advertisement
Add Comment
Please, Sign In to add comment