问题描述
我玩Python中的程序来创建十字绣计划和需要,以减少图像中的特定牙线颜色这样的。没必要使用所有的颜色从牙线调色板。在Python或伪code。
I am playing with a program in Python to create cross stitch schemes and need to reduce colors in an image to specific floss colors like this. Not necessary to use all the colors from the floss palette.On Python or Pseudocode.
自定义面板(中丸/枕头为例)不situable。有256种颜色最多,但牙线调色板拥有约450的颜色和我计划使用多个彩色图表来自不同制造商。
Custom palette (in PILL/Pillow for example) is not situable. There 256 colors max, but floss palette has around 450 colors and I plan to use multiple color charts from different manufacturers.
抖动也没有在十字绣situable。
Dithering also not situable in cross-stitching.
我觉得这可能是这样的:
I think this could be something like:
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形象。这是一个很大。)
May be there is a faster algorithm? (image_width * image_heigh * colors in palette = 112M delta_e calculations and comparisons on average 500x500px image. It's a lot.)
Dictonary对于已经计算delta_e?另一种算法/形式给出/优化?
Dictonary for already calculated delta_e? Another algorithm/aproach/optimization?
推荐答案
下面是memoize的一个例子。我也用内建的分
Here is an example of memoize. I've also used the builtin 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]
这篇关于如何降低图像的调色板到特定的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!