removing white background and have transparency instead of jagged edges

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
jeroen511
Posts: 3
Joined: 2019-06-21T13:21:57-07:00
Authentication code: 1152

removing white background and have transparency instead of jagged edges

Post by jeroen511 » 2019-06-21T14:00:22-07:00

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!
Last edited by jeroen511 on 2019-06-22T03:29:59-07:00, edited 2 times in total.

snibgo
Posts: 12306
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: removing white from background while preserving transparency

Post by snibgo » 2019-06-21T14:21:10-07:00

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
snibgo's IM pages: im.snibgo.com

jeroen511
Posts: 3
Joined: 2019-06-21T13:21:57-07:00
Authentication code: 1152

Re: removing white from background while preserving transparency

Post by jeroen511 » 2019-06-21T14:35:51-07:00

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?

jeroen511
Posts: 3
Joined: 2019-06-21T13:21:57-07:00
Authentication code: 1152

Re: removing white from background while preserving transparency

Post by jeroen511 » 2019-06-22T02:47:45-07:00

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");

Post Reply