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\'
I tried this:
from PIL import Image
import operator, itertools
def get_alpha_channel(image):
try:
alpha_index = image.getbands().index('A')
except ValueError:
# no alpha channel, so convert to RGBA
image = image.convert('RGBA')
alpha_index = image.getbands().index('A')
alpha_getter = operator.itemgetter(alpha_index)
return itertools.imap(alpha_getter, image.getdata())
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.