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

Sibling monks, I have a whole load of button images and what not, and I'm changing the colour scheme of a web site, and all I want to do is change every black pixel into a blue pixel, and then every grey pixel into a red one. Shd be easy? But I can't work out how to do it. Image::Magick seems to do everything else. But Colorize doesn't do what I want - e.g. I tried
use Image::Magick; my $image = Image::Magick->new; my $file = $image->Read('old.gif'); $image->Colorize(color => '#000000', fill => '#0000ff'); $image->Write('new.gif');
... and got an all blue image, rather than the same image but with the black lines in blue.

And I can't see anything else that might. I'd be most grateful if a kind monk cd tell me how to do this.

§ George Sherston

Replies are listed 'Best First'.
(wil) Re: Changing image colours - Image::Magick?
by wil (Priest) on May 13, 2002 at 14:44 UTC
    From the online docs for Image::Magick it seems that the function you're currently uses simply fills the entire image with your selected colour

    It looks to me like you need to look into the function ColorFloodfill, which replaces paticular pixels with your new specified colour.

    Hope this helps

    - wil
      Yes, I see that wd do what I want, *but* my image has several different, unconnected areas of black (i.e. letters) which I want to turn into blue, and as I read ColorFloodfill, I wd have to specify a pixel in each of these, which wd be laborious. Is it the right kind of laziness to want a method that will turn all black pixels blue??

      PS - what is the "color" parameter for in method Colorize? - the same thing happens whatever I set this to!

      § George Sherston
        Hi

        From what I can see you have to define the exact coordinates of the pixel that needs rendering. The same goes for Image::XPM and others.

        I hate to say this, but, do you have to use Perl for this solution? A lot of graphic packages have batch conversion tools these days. You could make changes to the colour pallete and batch convert a number of images from there.

        - wil
Re: Changing image colours - Image::Magick? - my solution
by George_Sherston (Vicar) on May 13, 2002 at 16:45 UTC
    So, realising that I'm not alone in not knowing how to do this I summoned up the courage to look through *all* the functions, no matter how oddly titled, and I found Opaque which does the business (arguably I shd have done this before - but I needed to have my back to the wall...).

    For the record, this does it:
    use Image::Magick; my $image = Image::Magick->new; my $file = $image->Read('old.gif'); $image->Opaque(color => '#000000', fill => '#0000ff'); $image->Write('new.gif');


    § George Sherston