[Numpy-discussion] reading 10 bit raw data into an array

Stefan van der Walt stefan at sun.ac.za
Mon Jul 30 10:32:44 EDT 2007


On Mon, Jul 30, 2007 at 04:01:46PM +0200, Danny Chan wrote:
> I'm trying to read a data file that contains a raw image file. Every pixel is
> assigned a value from 0 to 1023, and all pixels are stored from top left to
> bottom right pixel in binary format in this file. I know the width and the
> height of the image, so all that would be required is to read 10 bits at a time
> and store it these as an integer. I played around with the fromstring and
> fromfile function, and I read the documentation for dtype objects, but I'm
> still confused. It seems simple enough to read data in a format with a standard
> bitwidth, but how can I read data in a non-standard format. Can
> anyone help?

AFAIK, numpy's dtypes all have widths >= 1 byte.  The easiest solution
I can think of is to use fromfile to read 5 bytes at a time, and then
to use divmod to obtain your 4 values.

Cheers
Stéfan



More information about the NumPy-Discussion mailing list