这是我第一次使用numpy,我在一个地方遇到了一些麻烦。你知道吗
我有colors
,一个(xsize + 2, ysize + 2, 3)
数组,还有newlife
,一个(xsize + 2, ysize + 2)
布尔数组。我想在newlife
为真的所有位置的所有三个颜色值中添加一个介于-5和5之间的随机值。换句话说newlife
将2D向量映射到是否要在colors
中的该位置向颜色添加随机值。你知道吗
我试过无数种不同的方法:
colors[np.nonzero(newlife)] += (np.random.random_sample((xsize + 2,ysize + 2, 3)) * 10 - 5)
但我总是得到这样的东西
ValueError: operands could not be broadcast together with shapes (589,3) (130,42,3) (589,3)
我该怎么做?你知道吗
我想这正是你想要的:
这将更改
uint8
dtype的颜色。这两种假设并不重要:理解这一点最简单的方法是观察
colors[mask]
的形状。你知道吗相关问题 更多 >
编程相关推荐