Page 1 of 1

Check if an image has an alpha channel with IMv7

Posted: 2018-01-12T19:28:09-07:00
by traw
In IMv6, I could do something like:

Code: Select all

identify -format '%A' *.png
and it would give me True/False depending on whether the image has an alpha channel or not. But this doesn't seem to work with IMv7; instead it gives me "Blend" or "Undefined". Is there some way I can use to retrieve that information with IMv7?

BTW, the docs still say that %A returns a boolean.

Re: Check if an image has an alpha channel with IMv7

Posted: 2018-01-12T19:40:01-07:00
by fmw42
In IM 7, you should use magick identify. Same for all the other tools (such as montage, mogrify, etc), except for convert. Magick replace convert.

More importantly, IM 7 alpha is different from IM 6. It has a blend mode and a update mode. Undefined means no alpha is present. Blend is the normal -alpha set or -alpha on mode. Update comes when you use -alpha discrete.

See http://imagemagick.org/script/porting.php#cli

I would suggest you use %[channels] in its place. For example:

Code: Select all

magick identify -format "%[channels]\n" logo:
srgb

Code: Select all

magick identify -alpha set -format "%[channels]\n" logo:
srgba

Re: Check if an image has an alpha channel with IMv7

Posted: 2018-01-13T06:39:29-07:00
by traw
Thanks for the suggestion. One issue with "channels" is that it can return many different values. For example, I tried it on a collection of 30k images and here's all the different values I got: cmyk, gray, graya, srgb, srgba. So in order to determine if the image has an alpha channel, I would have to know all possible values for "channels" that contain alpha (e.g. graya, srgba) which is not ideal.

Re: Check if an image has an alpha channel with IMv7

Posted: 2018-01-13T11:12:11-07:00
by fmw42
Just check the last characters to see if it is an "a". Here I take the rose: image and enable the alpha channel.

Code: Select all

convert rose: -alpha set -format "%[channels]" info: | tail -c 1
a

You can still test for "undefined" by using %A in IM 7.

Re: Check if an image has an alpha channel with IMv7

Posted: 2018-01-13T11:29:32-07:00
by fmw42
Alternately, in Unix,

Code: Select all

[ "$(magick rose: -format "%A\n" info:)" = "Undefined" ] && echo "false" || echo "true"
false

or

Code: Select all

alpha=`magick rose: -format "%A\n" info:`
[ "$alpha" = "Undefined" ] && echo "false" || echo "true"
false

Code: Select all

[ "$(magick rose: -alpha set -format "%A\n" info:)" = "Undefined" ] && echo "false" || echo "true"
true

or

Code: Select all

alpha=`magick rose: -alpha set -format "%A\n" info:`
[ "$alpha" = "Undefined" ] && echo "false" || echo "true"
true

Re: Check if an image has an alpha channel with IMv7

Posted: 2018-01-13T16:22:11-07:00
by fmw42
Another way would be simply to check the verbose information for the file to find if it lists "Alpha"

Code: Select all

magick rose: -verbose info: | grep "Alpha"
returns nothing.

Code: Select all

magick rose: -alpha set -verbose info: | grep "Alpha"
Type: TrueColorAlpha
Alpha: 1-bit
Alpha:

Likewise with

Code: Select all

magick identify -verbose your image | grep "Alpha"

Re: Check if an image has an alpha channel with IMv7

Posted: 2018-01-13T17:02:13-07:00
by snibgo
The "check last character is 'a'" method isn't bomb-proof, eg:

Code: Select all

f:\web\im>%IM%convert rose: -colorspace OHTA -format "%[channels]" info:
ohta

f:\web\im>%IM%convert rose: -colorspace OHTA -alpha set -format "%[channels]" info:
ohtaa
However, the only format that can record OHTA (as far as I know) is MIFF, so I guess the method is usually good enough.

Re: Check if an image has an alpha channel with IMv7

Posted: 2018-01-13T19:11:03-07:00
by fmw42
Actually I was only thinking about external image formats. But point taken.

Nevertheless, OHTA is rather strange (as would be YCbCr), since there are only 3 channels (without alpha), but OHTA lists 4 letters (YCbCr lists 5 letters, but only has 3 channels). Even sRGB lists 4 letters for 3 channels.

So %[channels] is really more like colorspace[alpha]

%[channels] is really only reasonable for sRGB and CMYK (and possibly LAB) since those are probably the only colorspaces possible for external image formats.

Re: Check if an image has an alpha channel with IMv7

Posted: 2018-01-13T20:55:43-07:00
by traw
Thanks for the help. I think I'll just do a -format "%A\n" and check for "Blend" or "True" (to support IMv6).

Re: Check if an image has an alpha channel with IMv7

Posted: 2018-01-13T21:00:27-07:00
by fmw42
Or check for False (IM6) or Unspecified (IM7).