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.
Here is an example input:
Below is a very small 8x8 image that would be the outcome:
Here is a much larger version of the expected outcome::
Here is my code so far: It only works with black and white images.
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)) <= black[0] and out.getpixel((x,y)) >= 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")
And the output returned by my code:
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).
Thanks in advance!