<!DOCTYPE html><html><head><title></title><style type="text/css">p.MsoNormal,p.MsoNoSpacing{margin:0}</style></head><body><div>Hi Omry!<br></div><div><br></div><div>You're looking for `.view()`:<br></div><div><br></div><div>In [1]: import numpy as np<br></div><div>In [2]: b = np.arange(1, 13).astype(np.uint8)<br></div><div>In [4]: y = b.view(np.uint16).reshape((3, 2))<br></div><div>In [5]: y <br></div><div>Out[5]:<br></div><div>array([[ 513, 1027],<br></div><div> [1541, 2055],<br></div><div> [2569, 3083]], dtype=uint16)<br></div><div><br></div><div><br></div><div>You can also change the endianness by replacing `np.uint16` with `'>u2'`.<br></div><div><br></div><div>In [6]: z = b.view('>u2')<br></div><div>In [7]: z<br></div><div>Out[7]: array([ 258, 772, 1286, 1800, 2314, 2828], dtype=uint16)<br></div><div><br></div><div>Hope this helps!<br></div><div><br></div><div>Juan.</div><div><br></div><div><br></div><div>On Mon, 15 Jul 2019, at 12:45 PM, Omry Levy wrote:<br></div><blockquote type="cite" id="qt"><div dir="ltr"><div>Hi All,<br></div><div><br></div><div>I know how to reshape arrays, my problem is a little more complicated than that.<br></div><div>I am looking for the most efficient way to do the following and an example will help:<br></div><div><br></div><div>1) I have a an array of bytes b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]<br></div><div>This bytes array represents a width of 2 and a height of 3.<br></div><div>Each 2 bytes in that bytes array makes an unsigned 16 bit integer.<br></div><div><br></div><div>2) I want to get the following 2 dimensional array from the bytes array where each item in the two dimensional array is an unsigned 16 bit integer<br></div><div>something like:<br></div><div><br></div><div>[ <br></div><div> [(1,2) , (3,4)],<br></div><div> [(5,6) , (7,8)],<br></div><div> [(9,10) , (11,12)]<br></div><div>]<br></div><div><br></div><div>in the first row there are 2 elements each one is made of 2 bytes from the bytes array etc...<br></div><div>meaning that <br></div><div>(1, 2) = b[0] + b[1] << 8<br></div><div>(3, 4) = b[2] + b[3] << 8<br></div><div>...<br></div><div>...<br></div><div><br></div><div>What is the most efficient way to achieve that with numpy ?<br></div><div>Thanks<br></div><div>0L<br></div><div> <br></div><div><br></div><div><br></div><div><br></div><div> <br></div><div><br></div><div>[1] << 8 ,[2], [3, 4]<br></div></div><div>_______________________________________________<br></div><div>NumPy-Discussion mailing list<br></div><div>NumPy-Discussion@python.org<br></div><div>https://mail.python.org/mailman/listinfo/numpy-discussion<br></div><div><br></div></blockquote><div><br></div></body></html>