<div dir="ltr">Hi,<div><br></div><div>I have a question, regarding conversion of C (unsigned char *) buffer to a two dimensional numpy array</div><div><br></div><div>this is what i am doing:</div><div>1) I get a C network buffer of unsigned char *  let's call it the source buffer</div><div>the size of the source buffer is:</div><div>W * H * 2  bytes</div><div><br></div><div>2)  I am using <span style="font-family:monospace;font-size:15.6px;font-weight:700">PyByteArray_FromStringAndSize() </span>to convert the source buffer (a C unsigned char *) to python bytes array.</div><div>a = <span style="font-family:monospace;font-size:15.6px;font-weight:700">PyByteArray_FromStringAndSize(</span>source buffer, W * H * 2) </div><div><span style="font-family:monospace;font-size:15.6px;font-weight:700"><br></span></div><div>3) i am using numpy.frombuffer   to convert the python bytes array to a 1 dimensional numpy array of size W *H *2 bytes</div><div>b = numpy.frombuffer(a, <span class="gmail-n" style="color:rgb(51,51,51);font-size:12px;white-space:pre-wrap">dtype </span><span class="gmail-o" style="font-size:12px;white-space:pre-wrap;color:rgb(102,102,102)">= </span><span class="gmail-n" style="color:rgb(51,51,51);font-size:12px;white-space:pre-wrap">np</span><span class="gmail-o" style="font-size:12px;white-space:pre-wrap;color:rgb(102,102,102)">.</span><span class="gmail-n" style="color:rgb(51,51,51);font-size:12px;white-space:pre-wrap">uint8)</span></div><div><br></div><div>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</div><div>c = b.view(np.uint16).reshape((H, W))</div><div><br></div><div>Is there a way to optimize this some how ?</div><div>Can you suggest a faster and better solution ?</div><div><br></div><div>Thanks,</div><div><br></div><div><br></div></div>