diff options
Diffstat (limited to 'image_diff.pl')
-rw-r--r-- | image_diff.pl | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/image_diff.pl b/image_diff.pl new file mode 100644 index 0000000..9199f61 --- /dev/null +++ b/image_diff.pl @@ -0,0 +1,58 @@ +#!/usr/bin/perl -w + +# given 2 images of the same size, create a 3rd image, with the pixel +# at each (x,y) transparent if it's the same RGB color in both images, +# or set to the color in the 2nd image if they're different. + +# the 2nd image should be an edited version of the 1st image, and the +# output image can be displayed overlaid on the 1st image to show what +# the 2nd image looked like. + +use Image::Magick; + +sub die_usage { + die "usage: $0 <input-image1> <input-image2> <output-image>]\n"; +} + +sub readimage { + my $file = shift; + my $i = new Image::Magick; + my $r = $i->Read($file); + die "$r\n" if $r; + warn "read $file, OK\n"; + return $i; +} + +die_usage() if @ARGV != 3; +$a = readimage(shift); +$b = readimage(shift); + +$w = $a->Get('width'); +$h = $a->Get('height'); + +if($w != $b->Get('width') || $h != $b->Get('height')) { + die "input images are not the same pixel size\n"; +} + +$out = Image::Magick->new(size => $w . 'x' . $h); +$r = $out->ReadImage('xc:transparent'); +die "$r\n" if $r; + +# this is rather slow... +for($y = 0; $y < $h; $y++) { + for($x = 0; $x < $w; $x++) { + my @ap = $a->GetPixel(map => "RGB", x => $x, y => $y); + my @bp = $b->GetPixel(map => "RGB", x => $x, y => $y); + if($ap[0] != $bp[0] || $ap[1] != $bp[1] || $ap[2] != $bp[2]) { + push @bp, 65535; # fully opaque + } else { + @bp = (0, 0, 0, 0); # transparent (and black) + } + $r = $out->SetPixel(map => "RGBA", x => $x, y => $y, color => \@bp); + die "$r\n" if $r; + } +} + +$r = $out->Write($ARGV[0]); +die "$r\n" if $r; +warn "wrote to $ARGV[0]\n"; |