converting jpeg to transparent png

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
iscripts
Posts: 6
Joined: 2011-10-20T22:14:23-07:00
Authentication code: 8675308

converting jpeg to transparent png

Post 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..
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: converting jpeg to transparent png

Post 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.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply