I have created a structured array using numpy. Each structure represents the rgb value of a pixel.
I am trying to work out how to fill the array from a function but I keep getting an 'expected a readable buffer object' error.
I can set individual values from my function but when I try to use 'fromfunction' it fails.
I copied the dtype from the console.
Can anyone point out my mistake?
Do I have to use a 3 dimensional array as opposed to a 2d of structures
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 = ...does not try to fill the array. It assigns the output offromfunctionto that variable, replacing anything that was there before.fromfunction, on a line that deals withdtype. What does the documentation for this function say aboutdtype?