Convert ctypes 16 bit c_short array to a 32 bit numpy array
Wanderer
wanderer at dialup4less.com
Thu Mar 24 15:45:03 EDT 2011
On Mar 24, 3:14 pm, Wanderer <wande... at dialup4less.com> wrote:
> I'm using ctypes to have a dll fill a buffer with 16 bit data. I then
> want to convert this data to a numpy array. The code snippet below
> converts the data from 16 bit to 32 bit, but two 16 bit numbers are
> concatenated to make a 32 bit number and half the array is zero.
>
> Buffer = (c_short * byteSize)()
> self.cam.Qframe.pBuffer = cast(pointer(Buffer), c_void_p)
> perr = self.cam.GrabFrame()
> image1 = np.frombuffer(Buffer, int)
> xdim = self.cam.Qframe.width
> ydim = self.cam.Qframe.height
> image2 = image1.reshape(xdim, ydim)
>
> image2 looks like
>
> [[6291555 6357091 6160481 ..., 6488160 6226020 6553697]
> [6488163 6422625 6684770 ..., 6422624 6553697 6553696]
> [6488160 6357091 6226018 ..., 6815842 6422627 6553696]
> ...,
> [ 0 0 0 ..., 0 0 0]
> [ 0 0 0 ..., 0 0 0]
> [ 0 0 0 ..., 0 0 0]]
>
> How do convert 16 bit data to 32 bit data?
> Thanks
I figured it out.
Buffer = (c_ubyte * byteSize)()
self.cam.Qframe.pBuffer = cast(pointer(Buffer), c_void_p)
perr = self.cam.GrabFrame()
image1 = np.frombuffer(Buffer, np.uint16)
xdim = self.cam.Qframe.width
ydim = self.cam.Qframe.height
image2 = image1.reshape(xdim, ydim)
Though Eclipse thinks
Buffer = (c_ubyte * byteSize)()
is an error.
More information about the Python-list
mailing list