Storing compare output as 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
timAMD
Posts: 2
Joined: 2012-08-07T11:41:09-07:00
Authentication code: 67789

Storing compare output as a variable

Post by timAMD »

Hi all, I am forced to use perl to do some work on calculating the pixel percentage difference on two images and I cannot seem to get the result from compare to store in a variable. It just seems to always spit out to the shell (I am on windows) and therefore I can't do anything with the return (it is blank).

I am using "$wrong = `compare -metric AE -compose src -alpha off \"testGold\\\Atari_Car\\\Atari_car.1.tif\" \"test3.3.0_render\\\Atari_Car\\\Atari_car.1.tif\" null:`;"\

Can someone please help me with this? I would greatly appreciate it.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Storing compare output as a variable

Post by fmw42 »

add 2>&1 to the end of your command. The data goes to standard error and you need to make it go to standard out.
timAMD
Posts: 2
Joined: 2012-08-07T11:41:09-07:00
Authentication code: 67789

Re: Storing compare output as a variable

Post by timAMD »

That did the trick. Thanks
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Storing compare output as a variable

Post by anthony »

For image comapring in perl, for lots of images I found holding image thumbnails in memory with PerlMagick, and comparing them works better.

Here is a simple two thumb compare. I have scaled this up to compare directories of images.

Code: Select all

#!/usr/bin/perl
=head1 NAME

image_cmp_thumbs_two --  two image thumbnails - simply

=head1 SYNOPSIS

  image_compare  image  image

=head1 DESCRIPTION

Just compare two images and only two images.

=head1 AUTHOR

  Anthony Thyssen  21 January 2003  anthony_AT_griffith.edu.au

=cut
# -----------------------------------------------------------------------
use Image::Magick;

$i1 = Image::Magick->new;
$i2 = Image::Magick->new;

$w = $i1->Read( filename=> shift );       # read in images
warn("$w")  if $w;
exit  if $w =~ /^Exception/;

$w = $i2->Read( filename=> shift );
warn("$w")  if $w;
exit  if $w =~ /^Exception/;

$i1->Scale(width=>100, height=>100); # scale without preserving aspect ratio
$i2->Scale(width=>100, height=>100);

$w = $i1->Compare(image=>$i2);       # compare
die "$w" if $w;

printf "Errors is %f\n",     $i1->Get('error');
printf "Mean Error is %f\n", $i1->Get('mean-error');
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply