Page 1 of 1

Unwanted black background when converting PNG to JPG

Posted: 2008-08-08T03:56:04-07:00
by ajay_m
I am using the IM gelling example to create a PNG image, but the software I will be displaying this image in (VB6) does not natively support PNG, so I have invoked "convert" again to convert the PNG to a JPG. However when I do this, the JPG image is always rendered on a black background - this is innate to the converted image, and can be seen e.g when displayed in a web browser, although the PNG image from which it is derived is correctly shown on a white background.

If I alternatively convert the PNG to GIF, then the background is correctly rendered as transparent but now the edges of the object are visibly aliased, which they were not in the original PNG. (this may well be a GIF palette limitation).

I have been unable to find any combination of command-line options which will resolve either of these two issues. I'd prefer, really, to get JPG conversion to work, though.

Can anyone suggest what I am missing (tried -transparent-color, -antialias, -background, etc, etc with no luck). Any advice gratefully appreciated.

Re: Unwanted black background when converting PNG to JPG

Posted: 2008-08-08T11:03:29-07:00
by fmw42
PNG supports transparency and JPG does not. You may have black under transparent in PNG which gets converted to black. GIF supports transparency also, so it may work fine.

The best thing is to post your input image and your command line so that we can see for ourselves and see if there is a solution.

Re: Unwanted black background when converting PNG to JPG

Posted: 2008-08-09T04:19:33-07:00
by ajay_m
Hi, thanks for your prompt reply. There is no input image as I am using the gelling example exactly as specified in the IM examples, which uses the draw primitives to create a shape and then applies gel highlights. However in the final stage I then use convert to create a JPG from the PNG file that the example would have created. It is at this step that I cannot avoid the black background; possibly therefore I need to do something in creating the preceding PNG image but if you just use the exact example commands in the IM tutorial for gelling you are using the exact commands I am using, up to the final JPG conversion. (which in its simplest is just convert <png file> <jpg file>. I can't find any command-line options which assist in this final conversion but, as you suggest, perhaps in the preceding convert operation (which is currently exactly as per the IM gel example) I could specify some option which would make the PNG input file to the final JPG conversion more amenable e.g specify a white background. I will try this, but if you try the example you will see EXACTLY what I am trying to do.

(why I am trying to do this is that this would allow me to create images of medical drugs e.g capsules, in this case (because the gel example gave me this idea), and given a user's textual description of colours and markings, I can create, on the fly, an image of the drug. But first I need to resolve this issue, if possible. Then I will go on to create images of tablets and other medicines if the idea looks promising)

Re: Unwanted black background when converting PNG to JPG

Posted: 2008-08-09T05:56:53-07:00
by Bonzo
The black you are getting is the default colour that is created for the transparent section of the image.
I do not know what example you are following but as Fred said a sample image or a link to the tutorial would help.

I assume in the example there is either xc:none or -background none. This is setting the canvas colour to transparent try changing the none to white.

Re: Unwanted black background when converting PNG to JPG

Posted: 2008-08-11T00:20:06-07:00
by anthony
And mentions JPEG can't handle transparency, if you plan to save as JPEG you should 'flatten' the image onto the desired background color to remove the transparency.

See IM Examples, Common Image Formats, JPEG!
JPEG transparency - NOT
http://www.imagemagick.org/Usage/formats/#jpg_trans