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\'
# python 2.6+
import operator, itertools
def get_alpha_channel(image):
"Return the alpha channel as a sequence of values"
# first, which band is the alpha channel?
try:
alpha_index= image.getbands().index('A')
except ValueError:
return None # no alpha channel, presumably
alpha_getter= operator.itemgetter(alpha_index)
return itertools.imap(alpha_getter, image.getdata())