views:

192

answers:

1

After many hair-pulling frustrations I've finally got one version of the PerlMagick module working with my ActivePerl 5.10.0 build 1005. Now I'm playing with it to do some very basic color substitution.

I already can substitute one color ,say, black, with another, say, blue, using the following code:

#!perl
 use strict;
 use warnings;
 use Image::Magick;

  my $image = Image::Magick->new;
  $image->Read('color-test.bmp');
  $image->Opaque(fill => 'blue', color => 'black');
  $image->Write('result.bmp');

But I'm wondering if I can replace any color that is not black with the color blue. I hope and think there's some idiomatic syntax to achieve this, so I'm asking for a quick help :) Any ideas?

Thanks like always for any guidance/suggestions/comments :)

UPDATE

@rjh, thank YOU for the code and the information :) I tried 'em all with a little adpations and they all work like charm!

That older version cannot be run. My PerlMagick is 6.5.4, but with a little adapation, it also works like so:

  use strict;
  use warnings;
  use Image::Magick;

  my $image = Image::Magick->new;
  $image->Read('color-test.bmp');
     $image->Transparent(color=>'black');
     $image->Colorize(fill=>'blue');
     $image->Composite(image=>$image);
     $image->Write('result.bmp');

Well, of course I like your second version. It's super. It's the very syntax that I was expecting, hehe :)

And the first commandline version also works well although I was not expecting this version.

Again, thanks!

+7  A: 

In ImageMagick 6.3.7-10 and up, you can use the '+' form of opaque to invert the colour selection. This command will convert anything that is NOT black, with blue:

convert in.gif  -fill blue +opaque black   out.gif

In Perl this can be done via:

$image->Opaque(fill => 'blue', color => 'black', invert => 'True');

If you only have an older version it can still be done, via

convert out.gif \
      \( +clone -matte -transparent black \
         -fill blue  -colorize 100% \) \
      -composite    in.gif

...which I will leave for you to convert to the Perl API.

Source: http://www.imagemagick.org/Usage/color/#opaque - a useful resource for ImageMagick operations in general.

rjh
@rjh, I tried em all and they all worked like charm! Thanks alot!
Mike