How to get alpha value of PNG image with PIL?

How to determine if a PNG image has a transparent alpha channel or does not use PIL?

img = Image.open('example.png', 'r') has_alpha = img.mode == 'RGBA' 

Using the code above, we know if the PNG image has an alpha channel, but not how to get the alpha value?

I did not find the "transparency" key in the img.info dictionary, as described on the PIL website

I use Ubuntu and zlib1g, zlibc packages are already installed.

+17
python image transparent png python-imaging-library
Dec 26 '09 at 7:15
source share
5 answers

To get the alpha layer of an RGBA image, you just need to:

 red, green, blue, alpha = img.split() 

or

 alpha = img.split()[-1] 

And there is a way to set the alpha layer:

 img.putalpha(alpha) 

The transparency key is used only to determine the transparency index in palette mode (P). If you also want to close the transparency window of the palette mode and cover all cases, you could do this

 if img.mode in ('RGBA', 'LA') or (img.mode == 'P' and 'transparency' in img.info): alpha = img.convert('RGBA').split()[-1] 

Note. A conversion method is needed when image.mode is LA, due to an error in PIL.

+40
Dec 26 '09 at 11:06
source share

You can get alpha data from the whole image at a time by converting the image to a string with mode “A”, for example, this example will get alpha data from the image and save it as a grayscale image :)

 from PIL import Image imFile="white-arrow.png" im = Image.open(imFile, 'r') print im.mode == 'RGBA' rgbData = im.tostring("raw", "RGB") print len(rgbData) alphaData = im.tostring("raw", "A") print len(alphaData) alphaImage = Image.fromstring("L", im.size, alphaData) alphaImage.save(imFile+".alpha.png") 
+4
Dec 26 '09 at 11:10
source share
 # 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()) 
+3
Dec 26 '09 at 11:05
source share

img.info refers to the image as a whole - the alpha value in the RGBA image is a pixel, so of course it will not be in img.info . The getpixel method of the getpixel object, specified by the coordinate as an argument, returns a tuple with the values ​​(four, in this case) stripes for this pixel - the last value of the tuple will be A, alpha value.

+2
Dec 26 '09 at 7:31
source share

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 calculations to determine the mean and standard deviation, and the results were slightly different from the imagemagick fx:mean function.

Did the conversion change some values? I'm not sure, but it seems pretty trivial.

+1
Jan 12 '17 at 19:07
source share



All Articles