Hi again, folks...
The ultimate objective of the exercise here is to modify a truecolour image so the saturation of each pixel in the image is reduced.
With the following attempt, I'm trying to use GD to see what each pixel's RGB value is, change the value and set the pixel to the new RGB value.
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 loadi +ng 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_colo +ur, @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-accur +ate colours $image->setPixel($x, $y, $new_colour); printf(" AFTER: colour idx=%5d R=%3d, G=%3d, B=%3d\n\n", $new_co +lour, @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, va +lue => $v); my ($red, $green, $blue, $alpha) = $new_colour->rgba(); @$out_arr_ref = ($red, $green, $blue); return(1); } # end Update_Saturation
I'm a bit vague on the intricacies of GD... but I think I understand that the colour map for the image is used... So, that means a random/undefined colour (RGB) value wouldn't be found in the colour map, hence something like GD::Image->colorAllocate() would fail, which it does.
So, maybe there's a way to do this... or maybe I should be using something else to manipulate each pixel. I'm trying to avoid dealing with ImageMagick (at least within Perl), owing to its learning curve and 'difficulties' I'll have with installing it on my PC.
Using ActiveState Perl v5.20.2 under Windoze 8.1 32-bit and GD v2.53... if anyone can offer some clues on where to go next.
Thanks a heap.
In reply to Cannot Set Image Pixel to a Random Colour using GD by ozboomer
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |