use strict; use warnings; use GD; use Imager; # doesn't fail on non-existent file?... #my $image = GD::Image->newFromPng("rudddy.png") || die("cant on loading file\n"); my $image = GD::Image->newFromPng("ruddy.png") || die("cant on loading file\n"); $image->trueColor(1); my ($max_X, $max_Y)=$image->getBounds(); my $maxColors = $image->colorsTotal; my $X0 = int($max_X / 3); my $Y0 = int($max_Y / 3); for (my $x = $X0; $x < $X0+5; $x++) { for (my $y = $Y0; $y < $Y0+5; $y++) { #for (my $x = 0; $x < $max_X; $x++) { # for (my $y = 0; $y < $max_Y; $y++) { printf(" PIXEL: X=%03d Y=%03d\n", $x, $y); my $org_colour = $image->getPixel($x, $y); my @rgb = $image->rgb($org_colour); printf("BEFORE: colour idx=%5d R=%3d, G=%3d, B=%3d\n", $org_colour, @rgb); # Update_Saturation(\@rgb, \my @new_rgb); # REAL transformation @rgb[1] = 0; # ### TESTING ### transformation my @new_rgb = @rgb; # my $new_colour = $image->colorAllocate(@new_rgb); # Generates error; result = -1 my $new_colour = $image->colorClosest(@new_rgb); # Non-accurate colours $image->setPixel($x, $y, $new_colour); printf(" AFTER: colour idx=%5d R=%3d, G=%3d, B=%3d\n\n", $new_colour, @new_rgb); } # end: another row (y) } # end: another column (x) open(my $fh, ">", "new.png") || die("cant on output file\n"); binmode $fh; print $fh $image->png; exit(0); # ------------------------------------- # Update_Saturation - Reduce saturation of a pixel # Uses Globals: # ------------------------------------- sub Update_Saturation { my ($in_arr_ref, $out_arr_ref) = @_; my $colour = Imager::Color->new(@$in_arr_ref); my @hsv = $colour->hsv(); my ($h, $s, $v) = @hsv; $s = 0.7 * $s; my $new_colour = Imager::Color->new(hue => $h, saturation => $s, value => $v); my ($red, $green, $blue, $alpha) = $new_colour->rgba(); @$out_arr_ref = ($red, $green, $blue); return(1); } # end Update_Saturation