Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have a folder of png files that have transparent backgrounds. I would like to convert them from the command line and maintain the images' transparent backgrounds.

I'd appreciate any help.

Since jpgs don't support transparency so how can I specify the output jpg's background color's hex value? Right now it defaults to #ffffff? I have tried this script, but it gives the output jpgs white backgrounds:

mogrify -quality 100 -format jpg *.png

Update

I created these png files in inkscape. I should mention that my goal in converting these pngs to jpgs is to get rid of jagged edges on circular or angular objects. The pngs currently have jagged edges and I read that converting them to jpgs might solve this problem. However converting via the method outlined in Christian Skjødt's answer gives the images even worse jagged edges. Is there a way that I can convert the pngs to jpg and also use anti-aliasing or maybe another technique to get rid of the jagged edges? Thanks for the help.

share|improve this question
3  
It's quite difficult to answer your question if it changes all the time... For future reference: Try to include what you want to achieve in your question from the beginning. Re your "update 2": Is the "jaggedness" simply a result of your chosen resolution? What if you increase the resolution (the DPI number in the "Export bitmap..." dialog)? –  Marcel Stimberg Aug 29 '11 at 10:35

5 Answers 5

up vote 3 down vote accepted

To smooth the edges you could create the PNGs 10x larger than your target size, then resize with anti-aliasing to JPG. I have done this using Python and the Python Imaging Library to good effect, but I'm not sure how to do it using other tools.

share|improve this answer

The JPEG format does not support transparency -- therefore maintaining the transparent backgrounds is not possible if you want to (or have to) use the JPEG format. See Wikipedia for an overview of the capabilities of different image formats.

Note that the JPEG 2000 does indeed support transparency, but it is much less common than the JPEG format. It depends on your use case whether this is a viable alternative.

share|improve this answer
    
I may be wrong, but the OP is asking about setting to what color any transparent areas are made. They are being made white, but the OP could use blue, for instance. –  hexafraction Oct 22 '12 at 11:23

depending on what you're trying to do with the image, it might be possible to put the alpha channel to a separate grayscale image or a bitmask, and the program that are using the image would apply the auxiliary image as the translucent/transparent values. Although it's ultimately better to modify the program so it supports alpha png.

share|improve this answer

To your second question:

To specify the background color when converting your images you can use the -fill option:

mogrify -quality 100 -format jpg -fill "#FF0000" -opaque none *.png

Using -background provides similar results. Dont forget the -opaque none.

share|improve this answer
    
Hey, thanks for the help. Is there any way that I can antialias the output jpeg via this command? See update 2 above for additional context. Thanks. –  James Aug 26 '11 at 21:39

I'm sorry there is no way, jpeg format doesn't support an alpha channel so you can't have transparency.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.