4

I am using a software called ImageMagick, that has command line inputs to do photo comparisons and output a value. I want to save that output value to a specific variable for further analysis. How would I do that? Here is the command I would run in command line:

compare -metric MSE Picture1.jpg Picture2.jpg difference.png

This will compare the pixel differences between picture 1 and picture 2. Save it as difference and output a number of the difference. If identical it will be 0. I want to be able to capture that value in terms of a variable.

tripleee
  • 175,061
  • 34
  • 275
  • 318
user2128119
  • 105
  • 7
  • This is a really basic operation in shell scripting. You obviously need to start by reading a tutorial. – Barmar Apr 15 '16 at 23:16
  • 3
    See: [How to set a variable equal to the output from a command in Bash?](http://stackoverflow.com/q/4651437/3776858) – Cyrus Apr 15 '16 at 23:18
  • 1
    In shell scripting it's important to distinguish between a command's [stdout] _output_ and its _exit code_, which are separate aspects. With the benefit of hindsight: what you're looking for is the exit code. – mklement0 Apr 16 '16 at 03:23

5 Answers5

7

Worked for me:

variable=$(compare -metric MSE Picture1.jpg Picture2.jpg difference.png 2>&1)

or

variable=$(compare -metric MSE Picture1.jpg Picture2.jpg null: 2>&1)

user42
  • 71
  • 1
  • 2
3

As noted in a comment by Ignacio Vazquez-Abrams, compare actually reports the similarity status via its exit code, not via its stdout output.

Ignacio, in his own answer, also mentions that

  • the most recent command's exit code is reported in special variable $?
  • it's worth saving that exit code in a variable right away, so that subsequent commands don't overwrite it.

Also, exit code 0 indicates that images are similar, not identical.

Here's a code snippet that shows how to save the exit code and handle it subsequently:

compare -metric MSE Picture1.jpg Picture2.jpg difference.png # add >/dev/null for no output
ec=$?  # save exit code
case $ec in  # evaluate exit code
  0)
    echo "images are similar"
    ;;
  1)
    echo "images are dissimilar"
    ;;
  *)
    echo "an unexpected error occured"
    ;;
esac
Community
  • 1
  • 1
mklement0
  • 382,024
  • 64
  • 607
  • 775
1

Use command substitution.

variable=$(compare -metric MSE Picture1.jpg Picture2.jpg difference.png)
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 2
    That's what I thought they were asking as well, but then ["The compare program returns 2 on error otherwise 0 if the images are similar or 1 if they are dissimilar."](http://imagemagick.org/script/compare.php) – Ignacio Vazquez-Abrams Apr 15 '16 at 23:17
1

The result of a command will always be in $?. Make sure to save that value in another variable before executing another command or it will be overwritten.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
0

Or use backticks . AKA , command substitution

variable=`compare -metric MSE Picture1.jpg Picture2.jpg difference.png`

echo "$variable"
z atef
  • 7,138
  • 3
  • 55
  • 50
  • This should be comment on [Barmar's answer](http://stackoverflow.com/a/36658287/45375). While the backtick syntax works fine in this simple case, you should mentions its drawbacks compared to the modern `$(...)` syntax. – mklement0 Apr 16 '16 at 23:10