用imagemagick改变图像的颜色

时间:2021-08-28 08:59:45

I want to change the foreground colors of an image using rmagick/imagemagick. To be more specific: I want to convert the black or the white glyphicons-halflings icons(which are included in twitter bootstrap) into darkblue glyphicons-halflings icons. (It would be nice if I could specifie a hexcolor or RGB color.)

我想用rmagick/imagemagick改变图像的前景颜色。更具体地说:我想把黑色或白色的符号——halflings图标(包含在twitter bootstrap中)转换成深蓝色的符号——halflings图标。(如果我能说出一个六色或RGB颜色就好了。)

I have no idea if this is even possible but I clicked through the imagemagick documentation and the only thing I found is convert --size 100x100 xc:skyblue glyphicons-halflings.png -composite foo.png, the problem is that this only works when you specifie a size and that it is changing the foreground color not the background color. Besides it is skyblue not darkblue.

我不知道这是否可能,但我点击了imagemagick文档,发现的唯一东西是convert——大小为100x100 xc:skyblue字形符号-halflings。png复合foo。png的问题是,只有当你指定了一个大小,并且改变了前景颜色而不是背景颜色时,它才会工作。而且是天蓝色而不是深蓝色。

So anyone who has an idea how I could convert the white or the black glyphicons-halflings into blue glyphicons-halflings icons? (Bonuspoints for rmagick/ruby code snippets)

所以,有谁知道我如何将白色或黑色的象形文字转化成蓝色的象形文字?(rmagick/ruby代码片段的Bonuspoints)

3 个解决方案

#1


33  

Quick answer:

快速回答:

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

用imagemagick改变图像的颜色

Note: instead of blue, you can use any named colors, or RGB, or RGBA, etc (see http://www.imagemagick.org/script/command-line-options.php#fill).

注意:您可以使用任何已命名的颜色、RGB或RGBA等(参见http://www.imagemagick.org/script/command- options.php#fill)代替蓝色。

Explanation:

解释:

We "colorize" the icons in a two-step process:

我们将图标“上色”分为两步:

  • Extract the alpha channel from the icons; (note: the color of the original icons doesn't matter, we're extracting its alpha channel)
  • 从图标中提取alpha通道;(注意:原始图标的颜色不重要,我们提取它的alpha通道)
  • Apply the above mask over a colored background of your choice;
  • 在你选择的彩色背景上应用上面的遮罩;

For more information, read the following docs:

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

PS: The above solution has been researched while implementing jQuery ThemeRoller rewrite https://github.com/jquery/download.jqueryui.com/issues/77

以上解决方案是在实现jQuery ThemeRoller重写https://github.com/jquery/download.jqueryui.com/issues/77时研究的

#2


5  

From http://www.imagemagick.org/Usage/color_basics/#replace, you can replace all colors in the image that are not darkblue with darkblue with the following command:

从http://www.imagemagick.org/Usage/color_basics/#替换,您可以用以下命令将图像中所有非深蓝色的颜色替换为:

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

glyphicons-halflings进行转换。png -填充深蓝色+不透明的暗蓝色符号-halflings.png。

If you want to do it in ruby, I'd recommend using the Minimagick gem https://github.com/minimagic/minimagick which simply calls convert on the command line. From my experience, not all functionality has been ported from the command line to RMagick

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

#3


3  

In addition to sguha's answer - here's the Ruby version (albeit for QuickMagick):

除了sguha的答案——这是Ruby版本(尽管是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}")

#1


33  

Quick answer:

快速回答:

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

用imagemagick改变图像的颜色

Note: instead of blue, you can use any named colors, or RGB, or RGBA, etc (see http://www.imagemagick.org/script/command-line-options.php#fill).

注意:您可以使用任何已命名的颜色、RGB或RGBA等(参见http://www.imagemagick.org/script/command- options.php#fill)代替蓝色。

Explanation:

解释:

We "colorize" the icons in a two-step process:

我们将图标“上色”分为两步:

  • Extract the alpha channel from the icons; (note: the color of the original icons doesn't matter, we're extracting its alpha channel)
  • 从图标中提取alpha通道;(注意:原始图标的颜色不重要,我们提取它的alpha通道)
  • Apply the above mask over a colored background of your choice;
  • 在你选择的彩色背景上应用上面的遮罩;

For more information, read the following docs:

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

PS: The above solution has been researched while implementing jQuery ThemeRoller rewrite https://github.com/jquery/download.jqueryui.com/issues/77

以上解决方案是在实现jQuery ThemeRoller重写https://github.com/jquery/download.jqueryui.com/issues/77时研究的

#2


5  

From http://www.imagemagick.org/Usage/color_basics/#replace, you can replace all colors in the image that are not darkblue with darkblue with the following command:

从http://www.imagemagick.org/Usage/color_basics/#替换,您可以用以下命令将图像中所有非深蓝色的颜色替换为:

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

glyphicons-halflings进行转换。png -填充深蓝色+不透明的暗蓝色符号-halflings.png。

If you want to do it in ruby, I'd recommend using the Minimagick gem https://github.com/minimagic/minimagick which simply calls convert on the command line. From my experience, not all functionality has been ported from the command line to RMagick

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

#3


3  

In addition to sguha's answer - here's the Ruby version (albeit for QuickMagick):

除了sguha的答案——这是Ruby版本(尽管是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}")