Replace only background color of PNG

*爱你&永不变心* 提交于 2019-12-02 00:39:47

Essentially, you are seeking a "floodfill", like this:

convert in.png -fill green  -draw 'color 0,0 floodfill' result.png

That will look at the top-left pixel (0,0) and fill all similarly coloured pixels which are connected to it with green. If your background has slight variations in it, e.g. it's a JPEG, add some fuzz factor

convert in.jpg -fuzz 25% ...

Note that if your circle had touched the top and bottom edges, it would prevent the fill from flooding around to the right side of the diagram. So, let's say you had created your circle like this:

convert -size 100x60 xc:blue -fill blue -stroke black -draw "circle 50,30 50,0" in.png

And then you run the above command, you will get:

If that happens, you can add a single pixel wide border all the way around for the colour to "flow" through first, then flood-fill, and finally remove it later:

convert in.png -bordercolor blue -border 1 -fill green  -draw 'color 0,0 floodfill' -shave 1x1 result.png
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!