Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Does anyone know of a good way to compare two images using either perl 5.6 or 5.8? That's all I have to work with.

Replies are listed 'Best First'.
Re: compare images with perl 5.6 or 5.8
by hangon (Deacon) on Dec 27, 2008 at 04:46 UTC

    What is it you want to compare about the images? If its just a question of finding identical image files stored under different filenames, you can compare crc checksums of the files.

Re: compare images with perl 5.6 or 5.8
by Lawliet (Curate) on Dec 27, 2008 at 03:54 UTC

    After a quick google search, I have found a couple options. Use ImageMagick (there is a Perl module for it) or perhaps use the module Image::Compare.

    I am sure there are a lot more ways but I am too lazy to list them others may be better suited to help you in this area.

    And you didn't even know bears could type.

Re: compare images with perl 5.6 or 5.8
by zentara (Cardinal) on Dec 27, 2008 at 15:12 UTC
    You can use ImageMagick's compare. See PerlMagick page and search for Compare to do it in Perl. In shell, it would go something like
    compare -metric mae image.png reference.png difference.png
    in Perl, I havn't figured out the right syntax. If you do, please post it.

    Update: I found this on Google. The shell compare returns an image with red pixels where the diff's are. Apparently, Compare in PerlMagick just returns some statistics like the following returns:

    Errors is 3156.326700 Mean Error is 0.026116

    #!/usr/bin/perl use warnings; use strict; use Image::Magick; my $i1 = Image::Magick->new; my $i2 = Image::Magick->new; my $w = $i1->Read('image.png'); # read in images warn("$w") if $w; exit if $w =~ /^Exception/; $w = $i2->Read( 'reference.png'); 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');

    UPDATE2: Found a way with the Difference option of Composite, to produce a visual diff.

    #!/usr/bin/perl use warnings; use strict; use Image::Magick; my $i1 = Image::Magick->new; my $i2 = Image::Magick->new; #make some test files $i1->Set(size=>'250x100'); $i1->ReadImage('xc:white'); $i1->Draw(primitive=> 'rectangle', fill=>'red', points=>'60,30 110,70'); $i1->Write("i1.png"); #for comparison $i2->Set(size=>'250x100'); $i2->ReadImage('xc:white'); $i2->Draw(primitive=> 'line', fill=>'green', points=>'10,10 200,100'); $i2->Write("i2.png"); #for comparison $i1->Composite( gravity => "Center", compose=>'Difference', image => $i2, ); $i1->Write("$0-diff-compose.png");

    I'm not really a human, but I play one on earth Remember How Lucky You Are
Re: compare images with perl 5.6 or 5.8
by zentara (Cardinal) on Dec 27, 2008 at 17:14 UTC
    Imager does it too. Use the same i1.png and i2.png files from my example above, and this script will give the same output as ImageMagick.
    #!/usr/bin/perl use warnings; use strict; use Imager; my $img = Imager->new; $img->read(file=>'i1.png', type=>'png') or die "Cannot read: ", $img->errstr; my $img1 = Imager->new; $img1->read(file=>'i2.png', type=>'png') or die "Cannot read: ", $img1->errstr; my $out = $img->difference( other => $img1 ); $out->write(file=>"$0-diff.png", type=>'png') or die "Cannot write: ",$out->errstr;

    I'm not really a human, but I play one on earth Remember How Lucky You Are
      The versions of perl I have do not contain those modules. That's the trick. I know there are tons of libraries out there that do this but the problem is that my toolset I have to work with is very limited.
      I got the below output when tried to compare 2 png images. Cannot read: format 'png' not supported - formats bmp, ico, pnm, raw, sgi, tga available for reading Could you please help me to know the reason.
        The error means that when you compiled Imager, you didn't have libpng installed on your system, so png support was skipped. Install the full libpng ( some distros separate the development headers into something like libpng-devel. Just google for libpng, compile it, install it, then rebuild Imager.

        I'm not really a human, but I play one on earth.
        Old Perl Programmer Haiku