title: Convert image to specific palette using PIL without dithering
body: <p>I am trying to convert an RGB image in PNG format to use a specific indexed palette using the Pillow library (Python Image Library, PIL). But I want to convert using the "round to closest color" method, not dithering, because the image is pixel art and dithering would distort the outlines of areas and add noise to areas that are intended to be flat.</p>
  
  <p>I tried <code>Image.Image.paste()</code>, and it used the four specified colors, but it produced a dithered image:</p>
  
  <pre><code>from PIL import Image
  oldimage = Image.open("oldimage.png")
  palettedata = [0, 0, 0, 102, 102, 102, 176, 176, 176, 255, 255, 255]
  newimage = Image.new('P', oldimage.size)
  newimage.putpalette(palettedata * 64)
  newimage.paste(oldimage, (0, 0) + oldimage.size)
  newimage.show()    
  </code></pre>
  
  <p>I tried <code>Image.Image.quantize()</code> as mentioned in <a href="https://stackoverflow.com/a/9833540/2738262">pictu's answer to a similar question</a>, but it also produced dithering:</p>
  
  <pre><code>from PIL import Image
  palettedata = [0, 0, 0, 102, 102, 102, 176, 176, 176, 255, 255, 255]
  palimage = Image.new('P', (16, 16))
  palimage.putpalette(palettedata * 64)
  oldimage = Image.open("School_scrollable1.png")
  newimage = oldimage.quantize(palette=palimage)
  newimage.show()
  </code></pre>
  
  <p>I tried <code>Image.Image.convert()</code>, and it converted the image without dithering, but it included colors other than those specified, presumably because it used either a web palette or an adaptive palette</p>
  
  <pre><code>from PIL import Image
  oldimage = Image.open("oldimage.png")
  palettedata = [0, 0, 0, 102, 102, 102, 176, 176, 176, 255, 255, 255]
  expanded_palettedata = palettedata * 64
  newimage = oldimage.convert('P', dither=Image.NONE, palette=palettedata)
  newimage.show()
  </code></pre>
  
  <p>How do I automatically convert an image to a specific palette without dithering? I would like to avoid a solution that processes each individual pixel in Python, as suggested in <a href="https://stackoverflow.com/a/19389526/2738262">John La Rooy's answer</a> and comments thereto, because my previous solution involving an inner loop written in Python has proven to be noticeably slow for large images.</p>