Skip to content

Instantly share code, notes, and snippets.

@jes
Created February 17, 2018 21:11
Show Gist options
  • Save jes/dcb6ac7525b507648929c9b0f6de19af to your computer and use it in GitHub Desktop.
Save jes/dcb6ac7525b507648929c9b0f6de19af to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
use GD;
my $filename1 = shift or die "usage: xorimg ONE.PNG TWO.PNG > OUTPUT.PNG\n";
my $filename2 = shift or die "usage: xorimg ONE.PNG TWO.PNG > OUTPUT.PNG\n";
my $one = GD::Image->newFromPng($filename1, 1);
my $two = GD::Image->newFromPng($filename2, 1);
my ($w, $h) = $one->getBounds;
my $out = GD::Image->new($w, $h, 1);
for my $y (0 .. $h-1) {
for my $x (0 .. $w-1) {
my ($r1,$g1,$b1) = $one->rgb($one->getPixel($x, $y));
my ($r2,$g2,$b2) = $two->rgb($two->getPixel($x, $y));
put($out, $x, $y, ($r1^$r2), ($g1^$g2), ($b1^$b2));
}
}
print $out->png;
sub put {
my ($img, $x, $y, $r, $g, $b) = @_;
my $col = $img->colorAllocate($r,$g,$b);
$img->setPixel($x,$y,$col);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment