我尝试了这个脚本,用于替换图像中的单色。我需要重新着色图像中类似的颜色。例如,我有一个蓝色和青灰色。我想用红色重新着色这两种颜色。但是如下所述,我不能这样做。它可以在Python的PIL或OpenCV库中实现。
代码来源: 更改图像文件中特定RGB颜色像素为另一种颜色
代码来源: 更改图像文件中特定RGB颜色像素为另一种颜色
import numpy as np
from PIL import Image
image = Image.open('wall.jpg')
data = np.array(im)
r1, g1, b1 = 81, 90, 103 # Original value
r2, g2, b2 = 255, 0, 0 # Value that we want to replace it with
red, green, blue = data[:,:,0], data[:,:,1], data[:,:,2]
mask = (red == r1) & (green == g1) & (blue == b1)
data[:,:,:3][mask] = [r2, g2, b2]
im = Image.fromarray(data)
im.show()
data[ (data == color1).all(axis=2) ] = color2
其中color1 = 81, 90, 103
,color2 = 255, 0, 0
。 - undefined