remove background(any color) from image using image magick

Viewed 1433

Hi I am asking this with reference to this question Fuzzy flood fill in ImageMagic php - making a transparent image

 #!/bin/bash

# Get size of original
sz=$(convert -format "%wx%h" phone.png info:)

# Floodfill background area with transparency
convert phone.png -fuzz 5% -fill none -draw 'color 0,0 floodfill' ObjectOnTransparent.png

# Extract alpha channel
convert ObjectOnTransparent.png -alpha extract Alpha.png

# Extract edges of alpha channel - experiment with thickness
convert Alpha.png -edge 1 AlphaEdges.png

# Get difference from background for all pixels
convert phone.png \( +clone -fill white -colorize 100% \) -compose difference -composite Diff.png

# Multiply edges with difference, so only edge pixels will have a chance of getting through to final mask
convert AlphaEdges.png Diff.png -compose multiply -composite EdgexDiff.png

# Extend Alpha by differences at edges
convert Alpha.png EdgexDiff.png -compose add -composite ReEdgedAlpha.png

# Apply new alpha to original image
convert phone.png \( ReEdgedAlpha.png -colorspace gray \) -compose copyopacity -composite RemaskedPhone.png

# Splat RemaskedPhone over red background
convert -size $sz xc:red RemaskedPhone.png -composite Result.png

I ran these scripts and it is running accurate on images with white background and it is producing clean edges, but I want to ask if there is any way that I can remove any color background from an image like this image https://ibb.co/BNWnNBs from which I want to remove the black color from corners of image like this https://ibb.co/x7nD8Vb.

Any help is appreciated thanks

3 Answers

This works for me in ImageMagick 6 in Unix syntax.

Input:

enter image description here

convert eagle.png -alpha off -fuzz 10% -fill none -draw "matte 0,0 floodfill"  \
\( +clone -alpha extract -blur 0x2 -level 50x100% \) \
-alpha off -compose copy_opacity -composite \
eagle_transp.png

Result:

enter image description here

Windows Syntax would be:

convert eagle.png -alpha off -fuzz 10% -fill none -draw "matte 0,0 floodfill" ^ ( +clone -alpha extract -blur 0x2 -level 50x100% ) ^ -alpha off -compose copy_opacity -composite ^ eagle_transp.png

If in .bat script, double the % to %%

If you are filling with transparency, then change

convert phone.png -fuzz 5% -fill none -draw 'color 0,0 floodfill' ObjectOnTransparent.png

to

convert phone.png -fuzz 5% -fill none -draw 'matte 0,0 floodfill' ObjectOnTransparent.png

Then to antialias the edges, change

convert phone.png \( ReEdgedAlpha.png -colorspace gray \) -compose copyopacity -composite RemaskedPhone.png

to

convert phone.png \( ReEdgedAlpha.png -colorspace gray -blur 0x2 -level 50x100% \) -compose copyopacity -composite RemaskedPhone.png

To answer your question about the new image, that image is different in two ways. First, the background under the transparent pixels is not fully white. Second the white area does not go all around the image. So in ImageMagick, you need to do two extra steps.

Input:

enter image description here

You can see the first issue by turning alpha off on the image:

magick target-transp.png -alpha off target-transp_alpha_off.png

enter image description here

Note the black area in the top left corner. So in the processing we need to change that black area to white.

Second, as you can see in the above image, the white are does not go all around the image. So we need to pad the image with a border of white that is 1 pixel wide on all sides.

Thus you can do one of two processes:

  1. Flood fill the white

    magick target-transp.png
    -background white -alpha background -alpha off
    -bordercolor white -border 1
    -fuzz 10% -fill none -draw "alpha 0,0 floodfill"
    ( +clone -alpha extract -blur 0x2 -level 50x100% )
    -alpha off -compose copy_opacity -composite
    -shave 1x1
    target-transp_proc1.png

enter image description here

  1. or make all white pixels transparent including inside some of letters such as the insides of "a" and "e"

    magick target-transp.png
    -background white -alpha background -alpha off
    -bordercolor white -border 1
    -fuzz 10% -transparent white
    ( +clone -alpha extract -blur 0x2 -level 50x100% )
    -alpha off -compose copy_opacity -composite
    -shave 1x1
    target-transp_proc2.png

enter image description here

You will need to download the resulting images to see the differences.

Related