[SciPy-user] saving raw image

Zachary Pincus zachary.pincus@yale....
Thu Oct 16 07:14:54 CDT 2008

> Raw = fromstring(f0, uint16).astype(uint16)

Note that this is a tad unnecessary -- fromstring produces arrays with  
the requested dtype:

In : numpy.fromstring('abcd', dtype=numpy.uint16)
Out: array([25185, 25699], dtype=uint16)

However, it is easy to accidentally promote the output of math  
operations to different dtypes in math operations with signed int  
types or floats.

You best bet is to make sure that you convert the image back to 16-bit  
unsigned ints before saving:

# save image
Im_str = Im.astype(numpy.uint16).tostring(order='F')
f1 = file( 'Image1.raw', 'wb').write(Im_str)

Note that this conversion works like C casting, so if you've got  
negative values you will have wraparound.


More information about the SciPy-user mailing list