Hi,
An addition to this (old but useful) post, is the case where you
want to know if an image have some transparency data, or not.
Basically, you have to extract the Alpha channel, and count the numbers of colors in it.
If the count is greater than 1, then your alpha channel is used (it contains transparency data).
If the count is 1, then it have not transparency at all
(or it is fully transparent... but that is useless in the case of the images I process : they are never fully-transparent. And moreover, I'm only interested in knowing if an Alpha mask is at stake, or not).
For example, today, I had a PNG file holding an Alpha channel. But I needed to know if some transparent pixels were effectively present on this Alpha channel, or not.
You can be in presence of several types of files, and you cannot assume that they have transparency or not based on their extension.
A Png image can have an Alpha layer, but with all pixels visible. The same goes for a MIFF image.
So I share with the community the code below, that allows to extract the alpha channel, and to count the number of colors in it.
Code: Select all
c:\SOURCE.png ^
-set colorspace sRGB -alpha extract ^
c:\ALPHA-channel-of-PNG.miff
identify -format "%k" c:\ALPHA-channel-of-PNG.miff
ALL-IN-ONE ALTERNATIVE : It is possible to use the "-identify" operator in the same "convert" command. Please note that you will obtain the Number of colors directly + some basic information about the image. The number of colors is on line 2. I never succeded into obtaining ONLY the numbers of colors (line 2), and to get rid of the basic image information (line 1) : If someone have an idea.. thanks !
Using Windows Dos Command-line :
Code: Select all
convert ^
c:\SOURCE.png ^
-set colorspace sRGB -alpha extract ^
-identify -format "%k" info:
It throws me these 2 lines, where I would only need the Second on (stating
243 colors) :
Code: Select all
c:\SOURCE.png PNG 300x300 300x300+0+0 8-bit sRGB 24198B 0.016u 0:00.014
243
Any clue to this ?