Check if an image has an alpha channel with IMv7

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
traw
Posts: 3
Joined: 2018-01-12T18:30:31-07:00
Authentication code: 1152

Check if an image has an alpha channel with IMv7

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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
traw
Posts: 3
Joined: 2018-01-12T18:30:31-07:00
Authentication code: 1152

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

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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"
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

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

Post 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.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post 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.
traw
Posts: 3
Joined: 2018-01-12T18:30:31-07:00
Authentication code: 1152

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

Post 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).
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

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

Post by fmw42 »

Or check for False (IM6) or Unspecified (IM7).
Post Reply