ValueError:无法将大小为0的序列复制到维度为2的数组轴

2024-04-27 01:33:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我今天开始做numpy。 我正在创建一个numpy数组,并用给定的rgb填充它

rgb = (58,129,22)
xy = [0,0]
data = np.zeros( (43,21,2), dtype=np.uint8 )
print(xy)
data[xy[0],xy[1]] = rgb

但是当我运行它时,它会给出这个错误

[0, 0]
Traceback (most recent call last):
  File "C:\Users\admin\Desktop\y\Python\0\0.py", line 59, in <module>
    data[xy[0],xy[1]] = rgb
ValueError: cannot copy sequence with size 0 to array axis with dimension 2

我怎样才能解决这个问题


Tags: numpymostdata错误withnpzerosrgb
2条回答

好的,我发现了问题 rgb无法获取该值

什么是rgb?可以使用空列表(或元组)再现错误

In [101]: xy = [0,0]
     ...: data = np.zeros( (43,21,2), dtype=np.uint8 )
In [102]: data[xy[0],xy[1]]
Out[102]: array([0, 0], dtype=uint8)
In [103]: data[xy[0],xy[1]]=1
In [104]: data[xy[0],xy[1]]=[]
Traceback (most recent call last):
  File "<ipython-input-104-70fa3a2584fa>", line 1, in <module>
    data[xy[0],xy[1]]=[]
ValueError: cannot copy sequence with size 0 to array axis with dimension 2

3元素元组也不起作用:

In [113]: data[xy[0],xy[1]]=(58,129,22)
Traceback (most recent call last):
  File "<ipython-input-113-b469af2ddad0>", line 1, in <module>
    data[xy[0],xy[1]]=(58,129,22)
ValueError: cannot copy sequence with size 3 to array axis with dimension 2

相关问题 更多 >