Unwanted black background when converting PNG to JPG

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
ajay_m

Unwanted black background when converting PNG to JPG

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Unwanted black background when converting PNG to JPG

Post 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.
ajay_m

Re: Unwanted black background when converting PNG to JPG

Post 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)
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Unwanted black background when converting PNG to JPG

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

Re: Unwanted black background when converting PNG to JPG

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