Page 1 of 1

removing white background and have transparency instead of jagged edges

Posted: 2019-06-21T14:00:22-07:00
by jeroen511
Hello, I'm trying to remove the background from this image:
Image

I've read many forum posts where they use -transparent white, or replace the color with -opaque in combination with fuzz. but the problem is that you'll always end up with jagged edges.

I want to make all white in the image transparent and let the black lines fade off into into the transparent background. so that if I would overlay the line art over a black image, nothing (no white) would show up.

Would really appreciate some help with this, thank you!

Re: removing white from background while preserving transparency

Posted: 2019-06-21T14:21:10-07:00
by snibgo
Ignoring the logo bottom-right, Windows syntax:

Code: Select all

magick Xk9Wza0.png -negate ( +clone -fill Black -colorize 100 ) +swap -alpha off -compose CopyOpacity -composite out.png

Re: removing white from background while preserving transparency

Posted: 2019-06-21T14:35:51-07:00
by jeroen511
That's exactly it, thank you Snibgo!
Now I want to convert it to C++, but I'm not sure how to do this one, but more complex than I thought. Do you know to write that down?

Re: removing white from background while preserving transparency

Posted: 2019-06-22T02:47:45-07:00
by jeroen511
got it after some fiddling!

Code: Select all

Magick::Image img = Magick::Image("img.png");
img.negate(false);

Magick::Image imgBlack = img;
imgBlack.colorize(100, Magick::ColorRGB(0, 0, 0));
imgBlack.alphaChannel(AlphaChannelOption::OffAlphaChannel);

Magick::Image imgResult = imgBlack;
imgResult.composite(img, Magick::GravityType::CenterGravity, Magick::CompositeOperator::CopyAlphaCompositeOp);

imgResult.write("imgRemovedBg.png");