Page 1 of 1

replace color to transparent

Posted: 2008-11-25T01:11:38-07:00
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

Re: replace color to transparent

Posted: 2008-11-25T07:25:53-07:00
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

Re: replace color to transparent

Posted: 2008-11-25T07:57:27-07:00
by el_supremo
I've added a fifth example to the examples which shows how to use MagickTransparentPaintImage to target all white pixels.

Pete

Re: replace color to transparent

Posted: 2008-11-25T15:44:47-07:00
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.

Re: replace color to transparent

Posted: 2008-11-26T18:44:19-07:00
by nicolas1
del