【问题标题】:How to reduce image palette to specific colors?如何将图像调色板减少到特定颜色?
【发布时间】:2013-10-23 17:12:18
【问题描述】:

我正在使用 Python 中的一个程序来创建十字绣方案,并且需要将图像中的颜色减少为特定的牙线颜色 like this。不必使用牙线调色板中的所有颜色。 在 Python 或伪代码上。

Example

自定义调色板(例如在 PILL/Pillow 中)不合适。最多有 256 种颜色,但牙线调色板大约有 450 种颜色,我计划使用来自不同制造商的多种颜色图表。

抖动也不适用于十字绣。

我认为这可能是这样的:

result = []
for pixel_color in image:
    nearest = None
    for floss_color in floss_palette:
        distance = delta_e_cie2000(pixel_color, floss_color)
        if distance < nearest:
            nearest = floss_color
    result.append(nearest)

可能有更快的算法吗? (image_width * image_heigh * 调色板中的颜色 = 112M delta_e 计算和比较平均 500x500px 图像。很多。)

已经计算的 delta_e 的字典?另一种算法/方法/优化?

【问题讨论】:

  • memoize - 你不需要重新测试你已经完成的颜色

标签: python algorithm image-processing python-imaging-library color-palette


【解决方案1】:

这里是 memoize 的一个例子。我也使用了内置的min

def nearest(pixel_color, mem={}):
    if pixel_color in mem:
        return mem[pixel_color]
    n = min(floss_palette, key=lambda fc:delta_e_cie2000(pixel_color, fc))
    mem[pixel_color] = n
    return mem[pixel_color]

result = [nearest(pixel_color) for pixel_color in image]

【讨论】:

  • 看起来不错。然后我可以做PIL.Image.putdata(result)?是否有意义 1) available_pixel_color = PIL.Image.getcolors,2) 将 available_pixel_color 传递给 nearest 以制作字典,3) 将此字典应用于 pixel_color 并获得最终列表?
  • 您可以通过result = PIL.Image.eval(image, nearest) 将缩小应用于所有像素。
猜你喜欢
  • 2013-01-15
  • 2011-09-06
  • 2011-04-20
  • 2016-04-06
  • 2014-03-18
  • 1970-01-01
  • 1970-01-01
  • 2021-07-21
  • 1970-01-01
相关资源
最近更新 更多