Convert ctypes 16 bit c_short array to a 32 bit numpy array

Discussion in 'Python' started by Wanderer, Mar 24, 2011.

  1. Wanderer

    Wanderer Guest

    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
     
    Wanderer, Mar 24, 2011
    #1
    1. Advertising

  2. Wanderer

    Wanderer Guest

    On Mar 24, 3:14 pm, Wanderer <> 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.
     
    Wanderer, Mar 24, 2011
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. drife
    Replies:
    1
    Views:
    367
    Travis E. Oliphant
    Mar 1, 2006
  2. Duncan Smith
    Replies:
    3
    Views:
    423
    Duncan Smith
    Apr 25, 2007
  3. Replies:
    2
    Views:
    492
    Robert Kern
    Nov 13, 2007
  4. Replies:
    0
    Views:
    504
  5. W. eWatson
    Replies:
    2
    Views:
    942
    W. eWatson
    Nov 23, 2009
Loading...

Share This Page