Make transparent only large areas

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
ghostm
Posts: 2
Joined: 2011-12-28T09:01:31-07:00
Authentication code: 8675308

Make transparent only large areas

Post by ghostm »

Hi, is there any solution, allowing to convert only large areas of white color? I used -floodfill, maybe it would solve my problem, but I don't know how to use imagemagick to detect areas of white bigger than for example 5x5pixels. I have thousands of images containing multiple white areas, but simple use of -transparent option, even with -fuzz, leaves several ugly 1 pixel transparent points. Thanks in advance!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Make transparent only large areas

Post by fmw42 »

I am not quite sure what you are changing white to. So the following may or may not work without modification.

Use your floodfill or -fuzz as before so that you keep the white areas and the rest is make black so as to create a mask. Negate the mask. Then use -morphology open to remove white pixels in the mask that are small as defined by the kernel size and shape. Once you have cleaned up your mask, negate it and add it into the alpha channel of the image to make the black regions in the mask as transparent in your image.

Here is one example using the IM internal image logo:

convert logo: \
\( -clone 0 -fill black +opaque white -negate -morphology open disk:2 \) \
-alpha off -compose copy_opacity -composite logo_trans.png


It might be good idea to explain further and post a link to your image or some sample image that you want to test with and someone can likely create a full example of the processing. Also identify your IM version and platform.

The above example is unix syntax. If on Windows, see http://www.imagemagick.org/Usage/windows/ for syntax differences
ghostm
Posts: 2
Joined: 2011-12-28T09:01:31-07:00
Authentication code: 8675308

Re: Make transparent only large areas

Post by ghostm »

I'm changing white backgound color into 100% transparent. Your tip works better than my previous attempts, there are two of my testing images, but it still needs some improvements, especially with necklace:

http://allani.pl/system/images/000/044/ ... 1315673828
http://allani.pl/system/images/000/039/ ... 1313491434

ImageMagick 6.6.2-6, Ubuntu 11.04
Post Reply