【发布时间】:2020-09-18 08:58:42
【问题描述】:
我尝试使用此脚本替换图像的单一颜色。我需要在图像中重新着色相似的颜色。例如,我有蓝色,石板蓝。我想用红色重新着色这两种颜色。但正如我在下面提到的,我不能那样做。它可以在 python 中的 PIL 或 OpenCV 库中。
此代码来源:Change specific RGB color pixels to another color, in image file
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
标签: python image opencv graphics python-imaging-library