Skip to content

Instantly share code, notes, and snippets.

@jes
Created Feb 17, 2018
Embed
What would you like to do?
#!/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