convert white to transparent

Thursday, December 06, 2007 by Nate Murray.

I've had to work a lot with ImageMagick recently. Anyone who uses ImageMagick should definately know about Anthony Thyssen's ImageMagick tutorials.

My current project was to convert a white background to be transparent for thousands of product images. This was so that the product could be put on a gradiant background.

Note: because this article uses transparency I have converted the transparent images to be on a check background. The command to do this is:

 composite  -compose Dst_Over -tile pattern:checkerboard in.png out.png

My first attempt was to simply take all the white pixels and convert them to be transparent.

convert -fuzz .2% -transparent white tool.png ugly_fuzz.png

The problem with this one is that there is too much fuzz on the outside. My next attempt was to try to improve the situation by increasing the fuzz.

convert -fuzz 10% -transparent white tool.png removed_too_much.png

The problem with this approach is that this tool (and many others) has highlights that get removed if you just go for a blending of the white pixels. What we need is a way to use the -floodfill directive.

convert tool.png -bordercolor white -border 1x1 -matte -fill none -fuzz 7% -draw 'matte 1,1 floodfill' -shave 1x1 white_floodfill.png

Now thats more like it! Now some people may be satisfied to stop right there, but the pixel-halo really bothers me. I don't mind having some halo, but I'd much rather have something that is softer. The problem is we can't just use a straight blur because that would blur the picture as well.

ImageMagick supports masking. Which is basically a grayscale image that we use to specify the transparency. What we are going to do is create a black and white image of the silhouette of the tool and try remove the background based on that. If we can get the just the silhouette we may be able to blur the mask to give us a smooth edge but a keep a sharp image.

First what we are going to do is take the Difference of our image:

convert tool.png \( +clone -fx 'p{0,0}' \)  -compose Difference  -composite   -modulate 100,0  +matte  difference.png

Then we want to take the black portion and replace it with transparency:

convert difference.png -bordercolor white -border 1x1 -matte -fill none -fuzz 7% -draw 'matte 1,1 floodfill' -shave 1x1 removed_black.png

Then we want to extract the matte channel from this image.

convert removed_black.png -channel matte -separate  +matte matte.png

Then we need to negate the colors so that the mask we want is in the correct direction.

convert matte.png -negate -blur 0x1 matte-negated.png

Then we are ready to compose our matte onto our original image!

composite -compose CopyOpacity matte-negated.png tool.png finished.png

Beautiful.

Here is the full script to generate all of above:

cd images/im/masking
# doesnt work
convert -fuzz .2% -transparent white tool.png ugly_fuzz.png
composite  -compose Dst_Over -tile pattern:checkerboard ugly_fuzz.png ugly_fuzz_check.png

# doesnt work
convert -fuzz 10% -transparent white tool.png removed_too_much.png
composite  -compose Dst_Over -tile pattern:checkerboard removed_too_much.png removed_too_much_check.png

# works okay
convert tool.png -bordercolor white -border 1x1 -matte -fill none -fuzz 7% -draw 'matte 1,1 floodfill' -shave 1x1 white_floodfill.png
composite  -compose Dst_Over -tile pattern:checkerboard white_floodfill.png white_floodfill_check.png

# start real
convert tool.png \( +clone -fx 'p{0,0}' \)  -compose Difference  -composite   -modulate 100,0  +matte  difference.png

# remove the black, replace with transparency
convert difference.png -bordercolor white -border 1x1 -matte -fill none -fuzz 7% -draw 'matte 1,1 floodfill' -shave 1x1 removed_black.png
composite  -compose Dst_Over -tile pattern:checkerboard removed_black.png removed_black_check.png

# create the matte 
convert removed_black.png -channel matte -separate  +matte matte.png

# negate the colors
convert matte.png -negate -blur 0x1 matte-negated.png

# you are going for: white interior, black exterior
composite -compose CopyOpacity matte-negated.png tool.png finished.png
composite  -compose Dst_Over -tile pattern:checkerboard finished.png finished_check.png

» Post a Comment

Who we are:
The Pasadena Ruby Brigade is a group of programmers who are interested in Ruby. We enjoy sharing our ideas and code with anyone who is interested in this wonderful programming language.

Who can join:
Anyone! Just head over to our mailing list and drop us an email.

What we do:
We recently started a project over at RubyForge. This project is a group of programs written and maintained by our members that we thought could be beneficial to the whole community.

Projects

Downloads

Recent Posts

Archives