in reply to Re: Cannot Set Image Pixel to a Random Colour using GD
in thread Cannot Set Image Pixel to a Random Colour using GD

As I said, I'm admittedly pretty vague on GD... so I was just having a go with the tools I know about.

Convert::Color was something I'd not come across in my explorations... so that's been added to the toolkit. I'm pretty new at manipulating graphics (beyond simply getting size information and calling ffmpeg and similar CLI tools from within my code)... so I'm feeling my way a lot..

Further down the track, I'm trying to see if I can automatically 'tidy-up' some 100s (or 1000s) of smaller images so they all 'look' the same.. without going to the hassle of trying to learn about 'computer vision', OpenCV and such unless I really have to... particularly when I can conceive of a simpler way, even if it's slower or 'inelegant'(!).

I've modified the suggested code a bit so it works on my system (and I can understand it a bit more easily):-

use strict; use warnings; # unused ... use feature 'say'; # unnecessary? ... use open IO => ':raw'; use GD; use Convert::Color; # Handy.. Didn't know of this module my $saturation_factor = 0.9; # Will ultimately be coming from elsewhe +re # ---- sub desaturate { local $" = ','; my @HSV = Convert::Color-> new( "rgb8:@_" )-> as_hsv-> hsv; $HSV[ 1 ] *= $saturation_factor; return Convert::Color-> new( "hsv:@HSV" )-> as_rgb8-> rgb8 } # ---- # this is as clear as mud in a beer bottle to me... # my $gd = GD::Image-> newFromPngData( scalar qx/convert rose: png:-/, + 1 ); my $gd = GD::Image->newFromPng("ruddy.png", 1) || die("cant on loading + file\n"); # 1 = truecolour # my ($max_X, $max_Y) = $gd->getBounds(); # My version my $max_X = $gd->width; # TMTOWTDI my $max_Y = $gd->height; #for my $y ( 0 .. $gd->height - 1 ) { # Construct causes 'unknown + method' errors ()... # for my $x ( 0 .. $gd->width - 1 ) { for my $y ( 0 .. $max_Y - 1 ) { for my $x ( 0 .. $max_X - 1 ) { my $packed_rgb = $gd-> getPixel( $x, $y ); my @RGB = reverse unpack 'C3', pack 'L', $packed_rgb; $packed_rgb = unpack 'L', pack 'C3x', reverse desaturate( @RGB + ); $gd-> setPixel( $x, $y, $packed_rgb ) } } open my $fh, '>', 'new.png' or die; binmode $fh; print $fh $gd->png; close $fh;

Initially, I'm thinking about looking at (relative) luminance as a measure that I can try and match between images... so this code is only a step in the whole process.