#! perl -slw use strict; use GD; our $T ||= 150; my $file = shift @ARGV; die "$file not found" unless -e $file; my $in = GD::Image->new( $file ) or die $!; my( $w, $h ) = $in->getBounds; ## New palette image with just two colors my $out = GD::Image->newPalette( $w, $h ); my $black = $out->colorAllocate( 0, 0, 0 ); my $white = $out->colorAllocate( 255, 255, 255 ); for my $y ( 0 .. $h - 1 ) { for my $x ( 0 .. $w -1 ) { my( $r, $g, $b ) = $in->rgb( $in->getPixel( $x, $y ) ); ## Map the pixels relative to the threshhold my $index = (( $r = $g +$b ) / 3) > $T ? $black : $white; $out->setPixel( $x, $y, $index ); } } open OUT, '>:raw:perlio', "BW-$file" or die $!; print OUT $out->png( 9 ); close OUT; #system 1, "BW-$file"; ## display image