使用imagemagick更改图像的颜色

Mic*_*err 15 ruby image-manipulation rmagick imagemagick twitter-bootstrap

我想使用rmagick/imagemagick更改图像的前景色.更具体一点:我想将黑色白色的 glyphicons-halflings图标(包含在twitter bootstrap中)转换为深蓝色的glyphicons-halflings图标.(如果我可以指定十六进制或RGB颜色,那就太好了.)

我不知道这是否可能,但我点击了imagemagick文档,我发现的唯一convert --size 100x100 xc:skyblue glyphicons-halflings.png -composite foo.png的问题是,这只有在你指定一个大小并且它正在改变前景色而不是背景色时才有效.除此之外是天蓝色而不是深蓝色.

那么谁知道如何将白色或黑色的glyphicons-halflings转换为蓝色glyphicons-halflings图标?(rmagick/ruby​​代码片段的奖励点)

Raf*_*ier 35

快速回答:

convert glyphicons-halflings.png -alpha extract -background blue \
-alpha shape blue-glyphicons-halflings.png
Run Code Online (Sandbox Code Playgroud)

蓝glyphicons-halflings.png

注意:blue您可以使用任何命名颜色,RGB或RGBA等代替它们(请参阅http://www.imagemagick.org/script/command-line-options.php#fill).

说明:

我们通过两个步骤"着色"图标:

  • 从图标中提取Alpha通道; (注意:原始图标的颜色无关紧要,我们正在提取其alpha通道)
  • 将上面的面膜涂在您选择的彩色背景上;

有关更多信息,请阅读以下文档:

PS:上面的解决方案已经在实现jQuery ThemeRoller重写时进行了研究https://github.com/jquery/download.jqueryui.com/issues/77


sgu*_*uha 5

http://www.imagemagick.org/Usage/color_basics/#replace,您可以使用以下命令替换图像中未darkblue使用的所有颜色darkblue:

convert glyphicons-halflings.png -fill darkblue +opaque darkblue glyphicons-halflings.png

如果你想在ruby中做,我建议使用Minimagick gem https://github.com/minimagic/minimagick,它只需要convert在命令行上调用.根据我的经验,并非所有功能都已从命令行移植到RMagick