2012-08-28 162 views
15

我想使用rmagick/imagemagick更改图像的前景色。更具体地说:我想将blackwhiteglyphicons-halflings图标(包含在twitter引导程序中)转换为darkblue glyphicons-halflings图标。 (这将是很好,如果我可以指定一个hexcolor或RGB颜色。)使用imagemagick更改图像的颜色

我不知道这是甚至可能,但我点击了imagemagick文档,我发现唯一的东西是convert --size 100x100 xc:skyblue glyphicons-halflings.png -composite foo.png,问题是,这只在指定尺寸时才起作用,并且它正在改变前景色而不是背景色。除此之外,天空并不暗蓝。

那么,谁有一个想法,我怎么可以将白色或黑色的图标 - 半身人变成蓝色的图标 - 半身人像? (Bonuspoints为rmagick/Ruby代码段)

+0

如果你不能提供它们,:-) –

+0

唯一bonuspoints你可以* *兑现SE被称为“赏金” :-) –

+0

喜@迈克尔 - PERR,请参阅我的答案。它能解决你的问题吗?请让我知道。 ATT –

回答

33

快速回答

convert glyphicons-halflings.png -alpha extract -background blue \ 
-alpha shape blue-glyphicons-halflings.png 

blue-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

3

除了sguha的回答 - 这里红宝石版本(尽管是QuickMagick):

require 'quick_magick' 
source = "images/source.png" 
dest = "images/saved.png" 
foreground = "0000ff" #hex code for your colour 

QuickMagick.exec3("convert #{source} -fill \"##{foreground}\" +opaque \"##{foreground}\" #{dest}")