1

I already asked here but I just wonder if this could not be done natively with numpy because I will use numpy later anyway with cv2 that reads numpy arrays.

Background is, I read 13 chunks of 13680 bytes from a usb camera until I got 177840 bytes, that is one raw frame. I do this by appending the chunks:

frame = dev.read(0x81, 0x3570, tout)
frame += dev.read(0x81, 0x3570, tout)
...

The list is then filled with 0-255 values and two of that values shall form one 0-65535 value. So my question is how can I convert that (I assume) uint8 LSB list to an uint16 numpy array with half size of the list.

Lets assume I have that list:

list = [3,103,3,103]

I then want a numpy array with:

[26371, 26371]

or is there a way to straight away fill a numpy array with 13 reads from my usb device so that it has 177840/2 uint16 values at the end?

Paul G.
  • 461
  • 6
  • 21

1 Answers1

3

I maybe got it.

list = [3,103,3,103]
nlist = np.asarray(list, dtype='<B')
nlist = nlist.view(np.uint16)
print nlist, type(nlist[0])

output:

[26371 26371] <type 'numpy.uint16'>
Paul G.
  • 461
  • 6
  • 21
  • 1
    This works, but `nlist.dtype = np.uint16` is a better approach, as the data remains in place, only an attribute changes. Also, don't use `list` as the name of a variable: it's a built-in keyword (as in `list(some other collection)`), which will be shadowed by the variable name. –  Jun 28 '18 at 12:04
  • The .view(np.uint16) method here is what works! What the other comment suggest, just changing the dtype will not lead to the desired result, it will just change the type of each element, but the list will have the same lenght and not "combine" two consecutive elements. – Freya W Jun 13 '22 at 14:50