Page 1 of 1

converting jpeg to transparent png

Posted: 2012-05-09T05:02:57-07:00
by iscripts
hello guys..

here is the issue, i need to convert jpeg images(having white backgrounds) to transparent png images. this is the code being used at the moment:-

Code: Select all

system("convert img.jpg -fuzz 10% -transparent white img.png");
now the images do get converted, but it isnt perfect. most of the images end up with semi-transparent pixels in them. you tend to overlook it at first glance, but you get the issue when you impose the generated png on a dark background in photoshop. is there any way to make the conversion flawless?

this is a sample jpeg being used:-
Image

and this is the output:-
Image

thanks..

Re: converting jpeg to transparent png

Posted: 2012-05-10T16:40:16-07:00
by anthony
The problem with -transparent is that it is a all or nothing boolean color replacement. You get horrible alaised edges, and as you saw pixels that are the wrong color (anti-aliasing shades between the red and white).


I would look up the color that is that dark red, then use -level-colors to generate a white-black mask
That can then be converted to a transparency mask. I would color that mask the same red color.
All that is left is to re-add the inside black 'inside' parts.

An alternative is to use the script
http://www.imagemagick.org/Usage/scripts/bg_removal
which is based on the techniques of separating what should become inside, outside and semi-transparent areas of the image.
See Recovering Semi-Transparent Edges
http://www.imagemagick.org/Usage/masking/#semi-trans

You should be able to use a very small and very large fuzz factor for the separation.

Try it and let us know how you go. If you have trouble give us a bell.