我正在一个项目中,我想拍摄一个彩色网格的图片作为输入(在此示例中是用乐高积木制作的,并返回一个更小的修改后的图片。
以下是示例输入:
下面是一个很小的8x8图像,将是结果:
这里是预期结果的大版本::
[到目前为止,这是我的代码:它仅适用于黑白图像。
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")
和我的代码返回的输出:
“>
我的程序可以很好地用于黑白图像,但是我需要将其更改为可以使用几种颜色(红色,橙色,黄色,浅绿色,深绿色,浅蓝色,深蓝色,深蓝色,紫色,黑色和白色)的帮助。] >
提前感谢!
我正在一个项目中,我想拍摄彩色网格的图片作为输入(在此示例中是用乐高积木制作的),并返回更小的修改后的图片。这是一个示例输入:...
您做错了几件事。
首先,您应该使用PNG而不是JPG进行输出。 JPG引入了许多伪像,以至于像输出这样的小图像都完全退化了。
只是为了好玩,我用ImageMagick解决了这个问题-也可以从Python调用它...
首先,我创建了一个自定义调色板来匹配您的颜色-您的白色不是很白,您的绿色与ImageMagick的绿色概念不同,因此我使用十六进制代替它们来表示颜色。
Pixelator可解决问题。它是基于rr-
答案的程序包。对于AI和ML应用程序,它还具有一些其他好处。
重击