Replace only background color of PNG

别说谁变了你拦得住时间么 提交于 2019-12-20 03:13:45

问题


Here is my code:

#! /usr/bin/env sh
# Generate test image.
convert -size 100x60 xc:blue -fill blue -stroke black -draw "circle 50,30 55,55" in.png

# Make background transparent.
convert in.png -fill none -draw 'matte 0,0 floodfill' -flop  -draw 'matte 0,0 floodfill' -flop out.png
# Replace transparent background with green.
mogrify -background green -flatten out.png

# The wrong way.
convert in.png -transparent blue oops.png
mogrify -background green -flatten oops.png

It is based on this snippet: https://snippets.aktagon.com/snippets/558-how-to-remove-a-background-with-imagemagick

Starting with this:

I want to get this:

Not this:

Can I achieve this with a single convert command instead of a convert followed by a mogrify?

I am using ImageMagick 6.8.9-9.


回答1:


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


来源:https://stackoverflow.com/questions/39928633/replace-only-background-color-of-png

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!