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

I am currently using the Imager module for the manipulation of PNG files for a CGI script I have. I am having a heck of a time trying to find a way to simply replace a single colour with another throughout the image. There has to be a relatively simple way to do this. Any ideas? Thanks so much for your time. Aaron

Replies are listed 'Best First'.
•Re: Colour Replacement in a PNG
by merlyn (Sage) on Feb 02, 2004 at 20:33 UTC
Re: Colour Replacement in a PNG
by tachyon (Chancellor) on Feb 02, 2004 at 20:45 UTC
    See Skinning Images If you can use GIFs then Biochrome or Biotint will do what you want. If you want to change a color to say match a background (just a guess) then setting that color to transparent will also do the trick.

    It should be quite possible just to change the pallete entry for the color you want into the one you need if you want to have a little fun with pack and unpack.

    cheers

    tachyon

Re: Colour Replacement in a PNG
by PodMaster (Abbot) on Feb 03, 2004 at 07:08 UTC
      Thank you! Imager documentation leaves much to be desired, but after some tinkering I finally figured out how it worked. It does exactly what I need. Thanks again for your replies!

      Cheers!
      Aaron

        OK. So I can now replace one colour with another. I am now trying to make the replacement colour transparent. Say I want to replace white with red; I have the following code:
        my $redmap = [0 .. 255]; my $blumap = [0 .. 255]; my $grnmap = [0 .. 255]; $blumap->[255] = 0; $grnmap->[255] = 0; $img->map(red=>$redmap, blue=>$blumap, green=>$grnmap);
        This replaces all instances of (255,255,255) with (255,0,0). Now how do I make that red transparent? I thought I would need to do:
        my $redmap = [0 .. 255]; my $blumap = [0 .. 255]; my $grnmap = [0 .. 255]; my $alpha = [0 .. 255]; $blumap->[255] = 0; $grnmap->[255] = 0; $alpha->[0] = 255; $img->map(red=>$redmap, blue=>$blumap, green=>$grnmap, alpha=>$alpha);
        But that doesn't do anything. I like Imager for it's flexibility, but I am really getting frustrated with the documentation. Any help would be greatly appreciated.

        Cheers!
        Aaron

Re: Colour Replacement in a PNG
by parkprimus (Sexton) on Feb 02, 2004 at 22:08 UTC
    GD.pm might work well for you, althought I dont know all the details of your challenge.
    gd.pm

    --parkprimus