我用numpy创建了一个结构化数组。每个结构都表示一个像素的rgb值。如何从函数中填充一个numpy结构数组?
我想解决如何从一个函数中填充数组,但我不断收到'预计可读的缓冲区对象'错误。
我可以从我的函数中设置单个值,但是当我尝试使用'fromfunction'时失败。
我从控制台复制了dtype。
任何人都可以指出我的错误吗?
是否必须使用一个3维阵列,而不是结构
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')])
'pixel_output = ...'不会尝试填充数组。它将'fromfunction'的输出分配给该变量,替换之前存在的任何内容。 – hpaulj
您的错误的位置是相关的。它在'fromfunction'中出现在处理'dtype'的行上的几个级别。这个函数的文档有关'dtype'的说法是什么? – hpaulj
啊哈非常感谢。我没有发现 – user1714819