【发布时间】:2021-05-05 13:18:27
【问题描述】:
我创建了一个 python 函数来将图像颜色的每个像素转换为它的原色。它获取每个像素的 rgb 值的最大值,并创建一个只有该颜色的新列表。例如,如果一个像素的 rgb 值为 (250, 100, 23),它会创建一个值为 (256, 0, 0) 的新列表,因为 250 是最大值,并将其附加到一个 numpy 数组。结果如下:https://i.stack.imgur.com/o5Tbc.png,代码如下:
import matplotlib.pyplot as plt
import numpy as np
import skimage.data as data
def draw(list, img):
#the problem is in this function
arr = np.array(list)
arr.reshape(np.shape(img))
plt.imshow(arr)
plt.show()
def convert(image):
global x
#I made x global because I wanted to see if the function worked and it worked
x = []
for i in image:
for o in i:
if(max(o) == o[0]):
x.append([255, 0, 0])
elif max(o) == o[1]:
x.append([0, 255, 0])
elif max(o) == o[2]:
x.append([0, 0, 255])
draw(x, image)
convert(data.rocket())
#data.rocket() is an image that is a numpy array of shape 427, 640, 3
#data.rocket() is a perfect array that works with the function
【问题讨论】:
标签: python numpy matplotlib