简体   繁体   English

如何使用PerlMagick替换任何非特定颜色的颜色?

[英]How do I replace any color that is not a specific color using PerlMagick?

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. 在经历了许多令人沮丧的挫折之后,我终于得到了一个版本的PerlMagick模块与我的ActivePerl 5.10.0 build 1005一起使用。现在我正在玩它来做一些非常基本的颜色替换。

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 UPDATE

@rjh, thank YOU for the code and the information :) I tried 'em all with a little adpations and they all work like charm! @rjh,谢谢你的代码和信息:)我尝试了所有的一点点的吸引力,他们都像魅力一样工作!

That older version cannot be run. 旧版本无法运行。 My PerlMagick is 6.5.4, but with a little adapation, it also works like so: 我的PerlMagick是6.5.4,但有一点适应性,它也是这样的:

  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! 再次,谢谢!

In ImageMagick 6.3.7-10 and up, you can use the '+' form of opaque to invert the colour selection. 在ImageMagick 6.3.7-10及更高版本中,您可以使用“+”形式的opaque来反转颜色选择。 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: 在Perl中,这可以通过以下方式完成:

$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. ...我将留给您转换为Perl API。

Source: http://www.imagemagick.org/Usage/color/#opaque - a useful resource for ImageMagick operations in general. 来源: http//www.imagemagick.org/Usage/color/#opaque - 一般用于ImageMagick操作的有用资源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM