PIL 去除颜色区域之间的颜色混合
PIL remove colour mixing between colour regions
我的图片中只有很少的颜色。 (最多 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
。
我的图片中只有很少的颜色。 (最多 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
。