Richard Acock
Richard Acock

Reputation: 3

bash shell script to run imgcmp on two JPGs and store 'different' ones

I've got an IP camera that ftps files to a directory on my SuSE server.

I'm trying to write a shell script to do the following:

for every file in a directory;
    use image compare to check this file against the next one
    store the output in a file or variable.
    if the next file is different then  
        copy the original to another folder
    else 
        delete the original
end for

Running the following at the prompt generates this:

myserver:/uploads # imgcmp -f img_01.jpg -F img_02.jpg -m rmse > value.txt
myserver:/uploads # cat value.txt
5.559730
5.276747
6.256132
myserver:/uploads #

I know there's loads wrong with the code, the main issue I've got is with executing imgcmp from the script and extracting a value from it, so please point out the obvious as it may not be to me.

FILES=/uploads/img*
declare -i value
declare -i result
value = 10
shopt -s nullglob
# no idea what the above even does #
# IFS=.
# attempt to read the floating point number from imgcmp & make it an integer
for f in $FILES
do
  echo "doing stuff w/ $f"
  imgcmp -f 4f -F 4f+1 -m rmse > value.txt
  # doesn't seem to find the files from the variables #
  result= ( $(<value.txt) )
  if [ $result > $value ] ; then
    echo 'different';
    # and copy it off to another directory #
  else
    echo 'same'
    # and delete it #
  fi
  if $f+1 = null; then
    break;
  fi
done

when running the above, I get an error cannot open /uploads/img_023.jpg+1 and doing a cat of value.txt shows nothing, so all the files show as being the same.

I know where the issues are, but I've got no idea what I should actually be doing to extract the output of imgcmp (run from within a script) and then get it into a variable that I can compare it with.

Upvotes: 0

Views: 208

Answers (1)

Costi Ciudatu
Costi Ciudatu

Reputation: 38255

FILES=/uploads/*

current=
for f in $FILES; do
  if [ -z "$current" ]; then
    current="$f"
    continue
  fi
  next="$f"
  echo "<> Comparing $current against $next"
  ## imgcmp will return non-0 if images cannot be compared
  ## and print an explanation message to stderr;
  if result=$(imgcmp -f $current -F $next -m rmse); then
    echo "comparison result: " $result
    ## Checking whether the first value returned
    ## is greater than 10
    if [ "$(echo "$result" | awk '$1 > 10 {print "different"}')" = "different" ]; then
      echo 'different';
      # cp -v $current /some/other/folder/
    else
      echo 'same'
      # rm -v $current
    fi
  else
    ## images cannot be compared... different dimensions / components / ...
    echo 'wholly different'
    # cp -v $current /some/other/folder/
  fi
  current="$next"
done

Upvotes: 1

Related Questions