我使用 numpy 创建了一个结构化数组。每个结构代表一个像素的 rgb 值。
我正在尝试弄清楚如何从函数中填充数组,但我不断收到“预期的可读缓冲区对象”错误。
我可以从我的函数中设置单个值,但是当我尝试使用“fromfunction”时它失败了。
我从控制台复制了 dtype。
谁能指出我的错误?
我是否必须使用 3 维数组而不是 2d 结构
import numpy as np
#define structured array
pixel_output = np.zeros((4,2),dtype=('uint8,uint8,uint8'))
#print dtype
print pixel_output.dtype
#function to create structure
def testfunc(x,y):
return (x,y,x*y)
#I can fill one index of my array from the function.....
pixel_output[(0,0)]=testfunc(2,2)
#But I can't fill the whole array from the function
pixel_output = np.fromfunction(testfunc,(4,2),dtype=[('f0', '|u1'), ('f1', '|u1'), ('f2', '|u1')])