How to get alpha value of a PNG image with PIL?

后端 未结 5 1193
眼角桃花
眼角桃花 2020-11-30 02:46

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\'
         


        
5条回答
  •  一个人的身影
    2020-11-30 03:06

    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 in img.info. The getpixel 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.

提交回复
热议问题