【问题标题】:How to change picture background color using ImageMagick?如何使用 ImageMagick 更改图片背景颜色?
【发布时间】:2018-03-10 02:26:15
【问题描述】:

我想改变我的图片背景颜色,这是我的来源http://cdn.mayike.com/emotion/img/attached/1/image/dt/20170916/18/20170916180007_833.png

如果我想将背景颜色更改为另一种颜色,我该怎么办?

如果我想将背景颜色更改为透明,我该怎么办?

我尝试使用convert aa.png -background '#0e0e0e' bb.png,但这不起作用。

【问题讨论】:

    标签: 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
    

    【讨论】:

    • 感谢您提供有关绘图选项中的填充的提示!超级好用。
    • 对于具有透明背景的 PNG,convert original.png -background COLOR -flatten -alpha off transparent.png 为我完成了这项工作:stackoverflow.com/questions/9155377/…
    【解决方案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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-12-14
        • 1970-01-01
        • 2016-03-05
        • 2010-09-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多