简体   繁体   中英

writing uint16 Raw files

I try to write a raw file with the following code:

OutputFile = open(OutputFilePath,'w')
BlockArray= np.array(A).astype(np.uint16)
BlockArray.tofile(OutputFile)
OutputFile.close()

Then I try to read it again with:

OutputFile = open(OutputFilePath,'r+')
print np.fromfile(OutputFile,dtype=np.uint16)

The problem is that the file size is correct, but the first 8192 numbers are good, but then numbers get reversed or completely wrong. The shape of the array is (300,1312,98) . I don't know what is happening here. So can anyone explain to me way this happens and how I can write a correct raw file?

with reversed I mean that the hex number should be for example A5 01 = 421 but in the raw file number 01 A5 = 42241 is written.

The default behavior of the .tofile() method is to write a binary file, so you must open the file that you will write to in binary mode:

OutputFile = open(OutputFilePath, 'wb')

Likewise for reading the file:

OutputFile = open(OutputFilePath, 'rb')

This is crucial in Windows because of the automatic conversion of line endings that Python does with text (ie non-binary) files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM