我已经在SO上查看了一段时间可能有效的抗锯齿算法,但它们都会产生模糊的图像和/或不适用于我当前的方法。
我用Python和PIL创建了一个逐像素的过滤器,它创建了一个颜色调色板(它们的确定方式并不重要。)代码创建了一个如下所示的图像。正如你所看到的,它是非常混淆的。
http://i.imgur.com/BlGNzmP.png
我听说防图像的最佳方法是在绘制图像时进行,而不是之后;但是,我不确定这是否可以使用我当前的方法,即循环遍历每个像素。如果我可以在绘制图像时对其进行反锯齿的方法,请在此处发布。
以下是代码:
import Image, ImageDraw
from collections import Counter
def avg_color(arr):
r,g,b = 0,0,0
for item in arr:
r += item[0]
g += item[1]
b += item[2]
l = len(arr)
return (r//l,g//l,b//l)
COLOR_NUMBER = 12
PATH = "test.png"
COLORS = [
(255,255,255),
(0,0,0)
]
im = Image.open(PATH)
rgbim = im.convert('RGB')
size = rgbim.size
nim = Image.new('RGB', size)
draw = ImageDraw.Draw(nim)
for i in range(COLOR_NUMBER-2):
devs = [[765 for j in range(size[1])] for k in range(size[0])]
cols = [[765 for j in range(size[1])] for k in range(size[0])]
ind = []
for x in range(size[0]):
for y in range(size[1]):
r,g,b = rgbim.getpixel((x,y))
cols[x][y] = (r,g,b)
for c in COLORS:
diff = abs(c[0]-r)+abs(c[1]-g)+abs(c[2]-b)
devs[x][y] = min(diff,devs[x][y])
h = (0,0)
for x in range(size[0]):
for y in range(size[1]):
if devs[x][y] > devs[h[0]][h[1]]:
h = (x,y)
print "Added color",cols[h[0]][h[1]]
COLORS.append(cols[h[0]][h[1]])
print("Created palette.")
print(COLORS)
for x in range(size[0]):
for y in range(size[1]):
r,g,b = rgbim.getpixel((x,y))
devarray = []
col = COLORS[0]
for c in COLORS:
diff = abs(c[0]-r)+abs(c[1]-g)+abs(c[2]-b)
devarray.append(diff)
smallest = 0
for i in range(len(devarray)):
if devarray[i] < devarray[smallest]:
smallest = i
col = COLORS[smallest]
draw.point((x,y),fill=col)
print "Finished rough image."
nim.show()