0

I'm trying to count the number of distinct colors in an image using img.getcolors(). However, this does not distinguish between transparent and black pixels - they both report as one pixel colored [0,0,0].

How can I distinguish between transparent and black pixels? Many of the images I need to process are largely black on a transparent background.

For test purposes, I'm using a PNG I created which is half transparent, half black. len(img.getcolors()) is 1.

stickfigure
  • 13,458
  • 5
  • 34
  • 50
  • possible duplicate of [How to get alpha value of a PNG image with PIL?](http://stackoverflow.com/questions/1962795/how-to-get-alpha-value-of-a-png-image-with-pil) – Jongware Jul 07 '15 at 21:58
  • Not a duplicate. I read that other answer before I asked my question and it didn't help clear up the confusion. – stickfigure Jul 08 '15 at 07:41

1 Answers1

0

Embarrassing answer:

I was using convert('RGB') before calling getcolors(). Without the conversion, a 4-value tuple comes back with an alpha channel.

stickfigure
  • 13,458
  • 5
  • 34
  • 50