replace color to transparent

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
nicolas1

replace color to transparent

Post by nicolas1 »

Hello,

There is a 2 color image (text + background) PNG and it is necessary to replace background color (white) to transparent. How is it possible to do in magick core c api?

Thanks
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: replace color to transparent

Post by el_supremo »

It's easier to do this with MagickWand. See the third and fourth examples in my MagickWand Examples in C.
A floodfill may not do what's required if there are "islands" of background colour which must also be replaced. In that case you would use MagickTransparentPaintImage.

Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: replace color to transparent

Post by el_supremo »

I've added a fifth example to the examples which shows how to use MagickTransparentPaintImage to target all white pixels.

Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: replace color to transparent

Post by anthony »

As it is a gradient of two colors. this is easy.

first normalize or contrast-stretch the image to black and white. Then negate if needed to make opaque = white, black = transparent. now just run -background {color} -alpha shape the background color will be the shapes color and everything else will be transparent, or semi-transparent.

See http://www.imagemagick.org/Usage/basics/#alpha

ASIDE the above is equivelent to using CopyOpacity and re-seting the colors.

This is a LOT better than just making pure or near-pure white perfectly transparent! Though does not work well for GIF image saves.

You may also like to look at +level-colors if you just want to assign a new colors to black and white and have the gradient work out right.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
nicolas1

Re: replace color to transparent

Post by nicolas1 »

del
Post Reply