# 将图片中特定像素值的部分改为另一种像素值

## 改变颜色

height = shape[0]
width = shape[1]
dst = np.zeros((height,width,3))
for h in range(0,height):
for w in range (0,width):
(b,g,r) = img_array[h,w]
if (b,g,r)==(255,255,255):#白色
img_array[h,w] = (0,255,255)#蓝色


## 完整代码

代码片.

from PIL import Image
import numpy as np
img = Image.open("F:/PYproject/unet_camvid/CamVid/testimage/2.png")
#img.show()
img_array = np.array(img)#把图像转成数组格式img = np.asarray(image)
shape = img_array.shape
print(img_array.shape)
for i in range(0,shape[0]):
for j in range(0,shape[1]):
value = img_array[i, j]
#print("",value)
if value[0] != 0:
print("", value)
height = shape[0]
width = shape[1]
dst = np.zeros((height,width,3))
for h in range(0,height):
for w in range (0,width):
(b,g,r) = img_array[h,w]
if (b,g,r)==(255,255,255):#白色
img_array[h,w] = (0,255,255)#蓝色
if (b, g, r) == (85, 85, 85):  # 深灰
img_array[h, w] = (0, 128, 0)  # 绿色
if (b, g, r) == (170, 170, 170):  # 灰色
img_array[h, w] = (255, 255, 0)  # 黄色
if (b, g, r) == (0, 0, 0):  # 黑色
img_array[h, w] = (255, 0, 0)  # 红色
dst[h,w] = img_array[h,w]
img2 = Image.fromarray(np.uint8(dst))
img2.show(img2)
img2.save("3.png","png")



07-25
11-03 3万+
10-31 1万+
11-07 6104
04-04 1万+
08-18 1万+
03-01 3169
05-16 1万+