title: PIL - Dithering desired, but restricting color palette causes problems
body: <p>I am new to Python, and trying to use PIL to perform a parsing task I need for an Arduino project. This question pertains to the <code>Image.convert()</code> method and the options for color palettes, dithering etc.</p>
  
  <p>I've got some hardware capable of displaying images with only 16 colors at a time (but they can be specified RGB triplets). So, I'd like to automate the task of taking an arbitrary true-color PNG image, choosing an "optimum" 16-color palette to represent it, and converting the image to a palettized one containing ONLY 16 colors.</p>
  
  <p>I want to use dithering. The problem is, the <code>image.convert()</code> method seems to be acting a bit funky. Its arguments aren't completely documented (<a href="http://www.pythonware.com/library/pil/handbook/image.htm#Image.convert" rel="nofollow noreferrer">PIL documentation for Image.convert()</a>) so I don't know if it's my fault or if the method is buggy.</p>
  
  <p>A simple version of my code follows:</p>
  
  <pre><code>import Image
  MyImageTrueColor = Image.new('RGB',100,100) # or whatever dimension...
  
  # I paste some images from several other PNG files in using MyImageTrueColor.paste()
  
  MyImageDithered = MyImageTrueColor.convert(mode='P',
      colors=16,
      dither=1
      )
  </code></pre>
  
  <p>Based on some searches I did (e.g.: <a href="https://stackoverflow.com/questions/1065945/how-to-reduce-color-palette-with-pil">How to reduce color palette with PIL</a>) I would think this method should do what I want, but no luck. It dithers the image, but yields an image with more than 16 colors.</p>
  
  <p>Just to make sure, I removed the "dither" argument. Same output.</p>
  
  <p>I re-added the "dither=1" argument and threw in the Image.ADAPTIVE argument (as shown in the link above) just to see what happened. This resulted in an image that contained 16 colors, but NO dithering.</p>
  
  <p>Am I missing something here? Is PIL buggy? The solution I came up with was to perform 2 steps, but that seems sloppy and unnecessary. I want to figure out how to do this right :-)  For completeness, here's the version of my code that yields the correct result - but it does it in a sloppy way. (The first step results in a dithered image with >16 colors, and the second results in an image containing only 16 colors.)</p>
  
  <pre><code>MyImage_intermediate = MyImageTrueColor.convert(mode='P',
      colors=16
      )
  MyImageDithered = MyImage_intermediate.convert(mode='P',
      colors=16,
      dither=1,
      palette=Image.ADAPTIVE
      )
  </code></pre>
  
  <p>Thanks!</p>