9
$\begingroup$

How can I check if an Image has an alpha channel?


"What have you tried?"

  • AlphaChannel and ColorSeparate[..., "A"] return a result even if the image had no explicit alpha channel.

  • We could check the number of results from ColorSeparate or ImageChannels, but that also depends on ColorSpace, which may also be a ColorProfileData object (for which it isn't clear how to determine the number of expected channels).

$\endgroup$
10
$\begingroup$

I just discovered this command (in the Properties and Relations sections of the AlphaChannel command)

ImageMeasurements[img, "Transparency"]

It returns True if there is an alpha channel and False otherwise.

$\endgroup$
  • 2
    $\begingroup$ It's a weird place to put this functionality, isn't it? It relies on Image`ImageInformation which is a much more natural name for something like this. "Measurement" implies work/computation. Of course it was in the AlphaChannel documentation, so this is the kind of question that usually gets closed as "easily (?) found in the documentation". $\endgroup$ – Szabolcs Oct 9 '17 at 22:38
  • 1
    $\begingroup$ I was surprised at the whole ImageMeasurements command. It seems like an odd complement to ComponentMeasurements (which also takes images as input). $\endgroup$ – bill s Oct 9 '17 at 23:04
4
$\begingroup$

How about this:

RemoveAlphaChannel[img] == img

If img has no alpha channel, this returns True, but returns False if img has an alpha channel.

SetAlphaChannel[img] == img

seems to work too, but with the reverse logic.

$\endgroup$
  • 1
    $\begingroup$ This works. What I don't love about it is that the performance depends on the image size. There should be a constant time way. $\endgroup$ – Szabolcs Oct 9 '17 at 21:34
4
$\begingroup$

As of V12, Information can return this:

enter image description here

Information[ExampleData[{"TestImage", "Lena"}], "Transparency"]
False
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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