views:

276

answers:

1

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?

+2  A: 

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.

msw
Is it common for there to be an alpha channel with no data in it? If so, can I find that out?
brad
In my experience, people only create alpha channels when they want them to be non-empty, but there is a difference between what usually happens and what can happen. I wouldn't worry about it unless you find it popping up. I was just trying to be complete by noting the existence of an alpha channel does not necessarily imply the presence of transparency.
msw
Ah, yes. Never underestimate the user. Thanks!
brad