Transparency: Cutting Out Bordered Objects

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
thewickedd
Posts: 3
Joined: 2015-05-12T04:12:40-07:00
Authentication code: 6789

Transparency: Cutting Out Bordered Objects

Post by thewickedd »

Dear All,

I have images enclosed with a black anti-aliased edge on a white background. I have been reading the tutorials on how to remove the white background (only surrounding the image) and make it transparent, and have found http://www.imagemagick.org/Usage/masking/#bg_remove. However, i have the same problems with the floodfill as identified there, i end up with some bordering almost-white pixels.

Then i read in the next section, http://www.imagemagick.org/Usage/masking/#border_cut. It sounds very promising and applicable to my situation. If i can tell imageMagick that my background is white and the border black, it'll know how to deal with the fuzzy edges perfectly. However, sadly this section is under construction. How can I achieve this?

Sample image can be found here:
https://www.dropbox.com/s/yzexsn2sn5qvn ... 0.png?dl=0
https://www.dropbox.com/s/1v8k2bhw3mnff ... 0.png?dl=0

I am running the latest ImageMagick on windows 8.1 64bit: ImageMagick 6.9.1-2 Q16 x64 2015-04-14.

Thank you for your time and all the best!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Transparency: Cutting Out Bordered Objects

Post by fmw42 »

Note your file has a space before the period. I removed that. This works for me. Adjust the fuzz value as desired

Code: Select all

convert 001.png -fuzz 10% -fill none -draw "matte 0,0 floodfill" 001_trans.png
thewickedd
Posts: 3
Joined: 2015-05-12T04:12:40-07:00
Authentication code: 6789

Re: Transparency: Cutting Out Bordered Objects

Post by thewickedd »

Dear Fred,

Thanks for the reply, that is pretty close. However, when putting the image on a darker background, I can see some whitish pixels around its borders.

The http://www.imagemagick.org/Usage/masking/#border_cut section hints that it is possible to do transparency at the borders of the image perfectly, in that e.g. fully white would be fully transparent, and fully black fully opaque and the rest in between (I'm saving to png so have a full resolution alpha channel). The only thing I have found online that I hoped would be of relevance was the filltoborder option, but this appears to also be a boolean operation. Using

Code: Select all

convert 001.png -fuzz 90% -fill none -bordercolor black -draw "matte 0,0 filltoborder" 001_trans.png
I managed to get exactly the same result as you.

Is it currently possible to do such transparency modeling of the borders when you tell imagemagick the background and border colors, or is that bit in the manual under construction exactly because it isn't yet?

If not, that's ok, then I'll implement what I need with matlab.

Thanks a lot!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Transparency: Cutting Out Bordered Objects

Post by fmw42 »

I am not sure what you mean by "transparency modeling". You can use less fuzz value and extract the alpha channel as binary mask. Then use -morphology erode or dilate depending upon the binary polarity to remove pixels around the border. Then replace the alpha channel with the new trimmed mask.

This seems to work for me:

Code: Select all

convert 001.png -fuzz 10% -fill none -bordercolor black -draw "matte 0,0 floodfill"  \
-channel alpha -morphology erode octagon:1 +channel 001_trans.png
The larger the integer octagon value the more it will remove white aliased pixels around the border of the object.

Try adjusting both the fuzz value and the octagon value to get what you want
thewickedd
Posts: 3
Joined: 2015-05-12T04:12:40-07:00
Authentication code: 6789

Re: Transparency: Cutting Out Bordered Objects

Post by thewickedd »

Thanks Fred, that works pretty (very) well!
Post Reply