http://www.perlmonks.org?node_id=497355

johnnywang has asked for the wisdom of the Perl Monks concerning the following question:

Given an image, I'd like to replace some color (say red) with another color (say white). What's the best way to go about it? I've looked into PerlMagick (are there more complete documentations? the one at http://www.imagemagick.org/script/perl-magick.php doesn't explain function parameters in detail). I guess I could get the rgb value at every pixel and replace it one by one, isn't there something easier, or fuzzy(i.e., replace colors in certain rgb range)? Thanks.

Replies are listed 'Best First'.
Re: Replace colors in an image
by BrowserUk (Patriarch) on Oct 04, 2005 at 18:58 UTC

    It very much depends upon which image format, and which of the various subformats the image is in. Many images are stored in a "palettized format", meaning that there is a lookup table (palette) in the image file that maps the numbers stored in the data portion of the file to the actual colors used when the image is displayed. With this type of image, changing an individual colour is as simple as replacing it's entry in the palette with the new colour.

    If the image is stored as "true colour"--ie. 24-bits per perl format--then there is very little option but to scan the entire data portion of the image file and replace the old colour with the new.

    However, depending upon the image format you wish to process, this needn't be a particularly slow process. With knowledge of the format of the file and a little care, it is possible to do this a regex-style search and replace process, treating the appropriate portion of the image as a big string, thereby avoiding the overhead of (multiple) function/method calls per pixel.

    Which image format(s) do you need to process?


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
      just .png or .jpg, thanks.
Re: Replace colors in an image
by zentara (Archbishop) on Oct 04, 2005 at 21:04 UTC
    Imager is pretty good for that. For an example of manipulation see Tk ImageMap-color-zones. That just tints images. To replace some color, you could go thru every pixel, test for the rgb value to change, then adjust each pixel. I don't see anyway other than going thru the image pixel by pixel. Tinting is fast. Testing each pixel is slow.

    Here is a snippet to get pixel rgb values with GD, and I'm sure Imager has a similar method.

    #!/usr/bin/perl use GD; my $image = newFromPng GD::Image('z.png'); ($x,$y)=$image->getBounds(); $maxColors = $image->colorsTotal; $pixel=$image->getPixel(23,34); ($r,$g,$b)=$image->rgb($pixel); print "$r\t$g\t$b\n";
    And check out gif colour changes

    I'm not really a human, but I play one on earth. flash japh
Re: Replace colors in an image
by tonyc (Friar) on Oct 05, 2005 at 00:06 UTC

    A not so simple way of doing this in Imager:

    #!perl -w use strict; use Imager; my $from = shift; my $to = shift; my $in = shift; my $out = shift or die "Usage: $0 fromcolor tocolor inimage outimage\n"; my $from_color = Imager::Color->new($from) or die "Cannot convert fromcolor $from into a color: ", Imager->errs +tr, "\n"; my $to_color = Imager::Color->new($to) or die "Cannot convert tocolor $to into a color: ", Imager->errstr, +"\n"; my $img = Imager->new; $img->read(file=>$in) or die "Cannot read image $in: ", $img->errstr, "\n"; my $result = replace_color($img, $from_color, $to_color) or die "Cannot replace colors: ", Imager->errstr, "\n"; $result->write(file=>$out) or die "Cannot write image $out: ", $result->errstr, "\n"; sub replace_color { my ($img, $from_color, $to_color) = @_; my ($from_red, $from_green, $from_blue) = $from_color->rgba; my ($to_red, $to_green, $to_blue) = $to_color->rgba; my $rpnexpr = <<'EOS'; x y getp1 !pix @pix red from_red eq @pix green from_green eq @pix blue from_blue eq and and to_red to_green to_blue rgb @pix ifp EOS my %constants = ( from_red => $from_red, from_green => $from_green, from_blue => $from_blue, to_red => $to_red, to_green => $to_green, to_blue => $to_blue, ); return Imager::transform2({ rpnexpr => $rpnexpr, constants => \%constants }, $img); }