【问题标题】:How to change picture background color using ImageMagick?如何使用 ImageMagick 更改图片背景颜色?
【发布时间】:2018-03-10 02:26:15
【问题描述】:
【问题讨论】:
标签:
linux
shell
imagemagick
【解决方案1】:
由于您的图像不是二进制的,我认为您不会得到完美的结果。然而,在 Imagemagick 中,您有两种选择。首先,您可以将所有白色更改为红色:
convert 20170916180007_833.png.jpeg -fuzz 25% -fill red -opaque white -flatten result1.png
或者你可以做一个洪水填充来改变外部区域:
convert 20170916180007_833.png.jpeg -fuzz 25% -fill none -draw "matte 0,0 floodfill" -background red -flatten result2.jpg
【解决方案2】:
通常只需要 -opaque 或 -transparent。但是因为图片是黑白的,所以需要隔离ROI才能生效。我建议使用MVG 绘制命令。
如果换成另一种颜色,我该怎么办?
convert 20170916180007_833.png \
-fill '#0e0e0e' -fuzz 20% \
-draw 'color 0,0 floodfill' \
output_color.png
如果改成透明怎么办?
convert 20170916180007_833.png \
-fill 'transparent' -fuzz 20% \
-draw 'color 0,0 floodfill' \
output_transparent.png
【解决方案3】:
要应用任何背景颜色,首先程序应该知道边缘和背景。
您使用的图像不会那样做。
尽管您的命令是正确的,但它不起作用,因为没有区分边缘和背景。所以我们先用Masking
首先运行这个以获得边缘:
convert aa.png -bordercolor white -border 1x1 \
-alpha set -channel RGBA -fuzz 20% \
-fill none -floodfill +0+0 white \
-shave 1x1 aa.png
现在您将获得保存在 aa.png 中的边缘。此时您的背景是透明的。接下来运行背景颜色更改命令:
convert aa.png -background blue -flatten bb.png
现在您将获得预期的结果。
Output Final Image
来源:http://www.imagemagick.org/Usage/masking/#bg_remove