Hi Travis!<br>I guess I will still have to pad my data to full bytes before reading it, correct?<br><br><b><i>Travis Oliphant <oliphant.travis@ieee.org></i></b> schrieb:<blockquote class="replbq" style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; padding-left: 5px;"> Danny Chan wrote:<br>> Hi all!<br>> I'm trying to read a data file that contains a raw image file. Every <br>> pixel is assigned a value from 0 to 1023, and all pixels are stored from <br>> top left to bottom right pixel in binary format in this file. I know the <br>> width and the height of the image, so all that would be required is to <br>> read 10 bits at a time and store it these as an integer. I played around <br>> with the fromstring and fromfile function, and I read the documentation <br>> for dtype objects, but I'm still confused. It seems simple enough to <br>> read data in a format with a standard bitwidth, but how can I read data <br>> in a
 non-standard format. Can anyone help?<br>> <br><br>This kind of bit-manipulation must be done using bit operations on <br>standard size data types even in C.  The file reading and writing <br>libraries use bytes as their common denominator.<br><br>I would read in the entire image into a numpy array of unsigned bytes <br>and then use slicing, masking, and bit-shifting to take 5 bytes at a <br>time and convert them to 4 values of a 16-bit unsigned image.<br><br>Basically, you would do something like<br><br># read in entire image into 1-d unsigned byte array<br># create 16-bit array of the correct 2-D size<br># use flat indexing to store into the new array<br>#   new.flat[::4] = old[::5] + bitwise_or(old[1::5], MASK1b) << SHIFT1b<br>#   new.flat[1::4] = bitwise_or(old[1::5], MASK2a) << SHIFT2a<br>                     + bitwise_or(old[2::5], MASK2b) << SHIFT2b<br><br>#   etc.<br><br><br>The exact MASKS and shifts to use is left as an exercise for the
 reader :-)<br><br><br>-Travis<br>_______________________________________________<br>Numpy-discussion mailing list<br>Numpy-discussion@scipy.org<br>http://projects.scipy.org/mailman/listinfo/numpy-discussion<br></blockquote><br><p>


      <hr size=1>Jetzt Mails schnell in einem Vorschaufenster überfliegen. Dies und viel mehr bietet das <a href="http://de.rd.yahoo.com/evt=40590/*http://de.docs.yahoo.com/ymail/landing.html" target=_new> 
neue Yahoo! Mail</a>.