How to detect if a PNG image has transparent alpha channel or not using PIL?
img = Image.open('example.png', 'r')
has_alpha = img.mode == 'RGBA'
With above code we know whether a PNG image has alpha channel not not but how to get the alpha value?
I didn't find a 'transparency' key in img.info dictionary as described at PIL's website
I'm using Ubuntu and zlib1g, zlibc packages are already installed.
You can get the alpha data out of whole image in one go by converting image to string with 'A' mode e.g this example get alpha data out of image and saves it as grey scale image :)
To get the alpha layer of an RGBA image all you need to do is:
or
And there is a method to set the alpha layer:
The transparency key is only used to define the transparency index in the palette mode (P). If you want to cover the palette mode transparency case as well and cover all cases you could do this
Note: The convert method is needed when the image.mode is LA, because of a bug in PIL.
I tried this:
This returned the result that I was expecting. However, I did some calculation to determine the mean and standard deviation, and the results came out slightly different from imagemagick's
fx:mean
function.Perhaps the conversion changed some of the values? I'm unsure, but it seems relatively trivial.
The
img.info
is about the image as a whole -- the alpha-value in an RGBA image is per-pixel, so of course it won't be inimg.info
. Thegetpixel
method of the image object, given a coordinate as argument, returns a tuple with the values of the (four, in this case) bands for that pixel -- the tuple's last value will then be A, the alpha value.