我的图像中颜色很少。 (最多 5 种不同的颜色,而且都很明显。)
在颜色之间区域的边界处,有一条(通常是单像素宽)这些颜色之间的混合线。我怎样才能删除这个?
无论如何,我都会将它们作为 numpy 数组进行操作,所以如果可以使用 numpy 数组来实现这一点,那也是可行的。
例如这张图片:
这是由这个数组组成的:
image = np.append(
np.append(np.ones((10, 10, 3)), np.ones((1, 10, 3)) / 5., axis=0),
np.zeros((10, 10, 3)), axis=0
)
# Or a 10 * 10 white (0, 0, 0) region with a 1 * 10 line of (0.2, 0.2, 0.2) pixels
# underneath and a 10 * 10 black (1, 1, 1) region underneath that.
# Expected result:
expected = np.append(np.ones((10, 10, 3)), np.zeros((11, 10, 3)), axis=0)
我想要一个将 1 * 10 行灰色像素转换为黑色的函数,因为它更接近黑色。
最佳答案
您可以使用Image.quantize
强制所有颜色匹配您的调色板。这会将图像模式更改为 P
,完成后将其转换回 RGB
。
关于python - PIL 消除颜色区域之间的颜色混合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47678507/