title: Pixelate Image With Pillow
body: <p>I am working on a project where I want to take a picture of a colored grid as an input (made with Lego bricks in this example) and return a much smaller modified picture.</p>
  
  <p><strong>Here is an example input:</strong></p>
  
  <p><img src="https://i.stack.imgur.com/33z74.jpg" alt="Input"></p>
  
  <p><strong>Below is a very small 8x8 image that would be the outcome:</strong></p>
  
  <p><img src="https://i.stack.imgur.com/GR3PG.jpg" alt="Output"></p>
  
  <p><strong>Here is a much larger version of the expected outcome:</strong>:</p>
  
  <p><img src="https://i.stack.imgur.com/gkPqc.jpg" alt="Big Output"></p>
  
  <p><strong>Here is my code so far:</strong>
  It only works with black and white images.</p>
  
  <pre><code>from PIL import Image
  import re
  
  black = [(110,110,110),(0,0,0)] #The highest value and the lowest RGB value for the color black
  
  img = Image.open("input.jpg") #The input image
  size = (8,8) #The dimensions of the output image
  
  out = img.resize(size,resample=Image.LANCZOS) #Resize the image
  
  for y in range(size[0]): #loop through every pixel
      for x in range(size[1]):
  
          if out.getpixel((x,y)) &lt;= black[0] and out.getpixel((x,y)) &gt;= black[1]: #check to see if the pixel is within the accepted black values
  
              out.putpixel((x,y), (0,0,0)) #Give the current pixel true color
          else:
              #otherwise make the pixel black
              out.putpixel((x,y), (255,255,255)) #Give the current pixel true color
  
  """Save the pixelated image"""
  out.save("output.jpg")
  </code></pre>
  
  <p><strong>And the output returned by my code:</strong></p>
  
  <p><img src="https://i.stack.imgur.com/hHNrA.jpg" alt="Actual Output"></p>
  
  <p>My program works fine for black and white images, but I need help changing it to work with several colors (red, orange, yellow, light green, dark green, light blue, dark blue, purple, black and white).</p>
  
  <p>Thanks in advance!</p>