我正在开发一个项目,想要使用彩色网格的图片(例如用乐高积木制成)作为输入,并返回一个更小的修改后的图片。
下面是一个示例输入:
以下是一个8×8的非常小的图片输出结果:
下面展示了预期输出结果的大型版本:
这是我目前的代码:
它只适用于黑白图像。
下面是一个示例输入:
![Input](https://istack.dev59.com/33z74.webp)
![Output](https://istack.dev59.com/GR3PG.webp)
![Big Output](https://istack.dev59.com/gkPqc.webp)
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")
我的代码的输出结果:
我的程序可以处理黑白图像,但我需要帮助将其改为可以处理多种颜色(红色、橙色、黄色、浅绿色、深绿色、浅蓝色、深蓝色、紫色、黑色和白色)。
提前感谢!