Replace only background color of PNG

前端 未结 1 1023
梦谈多话
梦谈多话 2021-01-22 09:38

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 back         


        
相关标签:
1条回答
  • 2021-01-22 09:46

    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
    
    0 讨论(0)
提交回复
热议问题