#!/usr/bin/perl

use GD;
    my @files = `ls *.png`;




#open (PNG , "4001.png") || die $!;
#$image = GD::Image->new(\*PNG);
#@rgb = $image->rgb($image->getPixel(299,399));
#my $trans =  $image->colorClosest(@rgb);
#$image->transparent($trans);
#close PNG;
#
#open (PNG , ">4001.png") || die $!;
#binmode STDOUT;
#print PNG $image->png;	
#

my $i=0;
foreach(@files){
    s/\n|\r//g;
    my $base = $_;
    /([0-9]+)/;
    my $file = $1 . ".png";
    #print $file . "\n";
    open (PNG , $file) || die $!;
    my $image = GD::Image->new(\*PNG) ;
    eval{
	@rgb = $image->rgb($image->getPixel(299,399));
    };
    if($@){
	print $file . "/" . $?
    }
    else{
	$trans =  $image->colorClosest(@rgb);
	$image->transparent($trans);
	close PNG;
	open (PNG , ">$file") || die $!;
	binmode STDOUT;
	print PNG $image->png;	
	close PNG;
    }

    #$i++;
    #last  if $i == 3;
	
}
