Detect Alpha Channel with ImageMagick

brad picture brad · Apr 6, 2010 · Viewed 18.2k times · Source

Scenario

I would like to save images with alpha transparency as .png and images without alpha transparency as .jpg (even if their original format is .png or .gif). How can I detect whether or not an image has alpha transparency using ImageMagick?

Answer

msw picture msw · Apr 6, 2010

The ImageMagik command:

identify -format '%[channels]' foo.png 

will print rgba or rgb if there is or is not an alpha channel, respectively.

There could be an alpha channel present with no data in it which wouldn't actually have any transparency, but that is a bit more complicated.