[Numpy-discussion] converting a C bytes array to two dimensional numpy array

Derek Homeier derek at astro.physik.uni-goettingen.de
Tue Jul 16 08:48:02 EDT 2019

On 16 Jul 2019, at 9:30 am, Omry Levy <omrylevy at gmail.com> wrote:
> I have a question, regarding conversion of C (unsigned char *) buffer to a two dimensional numpy array
> this is what i am doing:
> 1) I get a C network buffer of unsigned char *  let's call it the source buffer
> the size of the source buffer is:
> W * H * 2  bytes
> 2)  I am using PyByteArray_FromStringAndSize() to convert the source buffer (a C unsigned char *) to python bytes array.
> a = PyByteArray_FromStringAndSize(source buffer, W * H * 2) 
> 3) i am using numpy.frombuffer   to convert the python bytes array to a 1 dimensional numpy array of size W *H *2 bytes
> b = numpy.frombuffer(a, dtype = np.uint8)
> 4) i am creating a 2 dimensional numpy array from (3) when each element in that array is made of 2 bytes from the python bytes array
> c = b.view(np.uint16).reshape((H, W))
> Is there a way to optimize this some how ?
> Can you suggest a faster and better solution ?
The PyByteArray conversion seems unnecessary - if you can access your input as a buffer,
calling np.frombuffer on it directly with the correct dtype should work just as well, and you
can reshape it on the fly:

c = np.frombuffer(source_buffer, dtype=np.uint16, [count=W*H]).reshape((H, W))

The optional ‘count’ argument would only be required if you cannot simply read the buffer
to its end.


More information about the NumPy-Discussion mailing list