title: How to know what colors PIL is referring to when getting all unique colors in a geoTIFF?
body: <p>I have a bunch of geoTIFF images I am trying to analyze. For example, here is one below:</p>
  <p><a href="https://i.stack.imgur.com/1QgAe.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1QgAe.png" alt="esiber_2000jul28a_3c" /></a></p>
  <p>I would like to obtain a count of how many pixels in this image are blue, and how many are white. I am using the following code to obtain this information:</p>
  <pre class="lang-py prettyprint-override"><code>from PIL import Image
  from collections import defaultdict
  
  # open the image (fp is the filepath of the image)
  im = Image.open(fp)
  # the image is in 'P' mode
  
  # convert to RGB?            
  # im.convert('RGB')
              
  by_color = defaultdict(int)
  for pixel in im.getdata():
      by_color[pixel] += 1
  
  im_col = Image.Image.getcolors(im)
  print(im_col)
  print(by_color)
  </code></pre>
  <p>These images are in 'P', or palettized mode. Whether or not I convert these images to 'RGB' mode by uncommenting that line of code above, I get the following output in this format (this specific output is for the example image above):</p>
  <pre><code>[(777, 0), (196, 1), (378, 2), (1149, 3)]
  defaultdict(&lt;class 'int'&gt;, {2: 378, 3: 1149, 1: 196, 0: 777})
  </code></pre>
  <p>So, from my understanding, the 0, 1, 2, and 3 represent the unique pixel colors, and the respective 777, 196, 378, and 1149 represent the amount of times these pixels are in the image (and if I add up 777 + 196 + 378 + 1149 = 2500, this makes sense, since this is a 50 pixel x 50 pixel image).</p>
  <p>My two main concerns are:</p>
  <ol>
  <li>Why are there four values? According to the image, there should only be three values: black background, blue ocean, and white ice - is there a hidden color somewhere?</li>
  <li>How can I know what colors the 0, 1, 2, and 3 represent in palletized format? I looked for some color code on some website, and I found that 0 meant 2, but the 1 meant green, which I know isn't right, so I'm definitely misunderstanding something here. Is there a way to get this 'color code' from the image using PIL?</li>
  </ol>
  <p>Thanks in advance.</p>