Assigning compare output to a variable

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
ninaw
Posts: 2
Joined: 2015-09-24T05:40:51-07:00
Authentication code: 1151

Assigning compare output to a variable

Post by ninaw »

I'm running a script to compare images but want the script to do things based on the level of difference. How on earth so I assign the output of the compare command to a variable, and if possible convert it to an integer (rounded or truncated, it doesn't really matter)? So far I have tried various syntax for treating it like a normal operation. None have worked.
I'm running a Linux terminal window on Ubuntu 14.04

I have tried
difference=$((compare -metric PSNR file1.png file2.png diff.png))
and
difference=`compare -metric PSNR file1.png file2.png diff.png`
and various others.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Assigning compare output to a variable

Post by snibgo »

The text is written to stderr, not stdout. Put

Code: Select all

2>&1
before the close backtick.
snibgo's IM pages: im.snibgo.com
ninaw
Posts: 2
Joined: 2015-09-24T05:40:51-07:00
Authentication code: 1151

Re: Assigning compare output to a variable

Post by ninaw »

Many thanks. That has solved the problem, and fortunately before I pulled all my hair out.
Post Reply