Numpy Memmap遇WinError8求助:读取raw byte文件转为指定数组
Hey there! First off, huge kudos for finally joining the conversation after six years of using Stack Overflow—we’re stoked you’re reaching out with your question. Let’s get this raw file sorted out for you.
First, let’s confirm the numbers check out: your target shape (613, 640, 224) has a total of 613 * 640 * 224 = 87,879,680 elements. Since each np.uint16 takes up 2 bytes, that’s exactly 87,879,680 * 2 = 175,759,360 bytes—matching your file size perfectly. That’s a good start!
Here’s a straightforward Python/Numpy workflow to read your file into the desired array:
import numpy as np # Replace with your actual file path raw_file_path = "your_file.raw" # Step 1: Read the raw bytes directly as uint16 values raw_data = np.fromfile(raw_file_path, dtype=np.uint16) # Quick sanity check to ensure we have the right number of elements assert raw_data.size == 613 * 640 * 224, "File size doesn't match the target shape's element count!" # Step 2: Reshape into the Fortran-ordered array target_array = raw_data.reshape((613, 640, 224), order='F') # Optional: Verify the array is indeed Fortran-contiguous print(f"Array is Fortran-ordered: {target_array.flags.f_contiguous}")
A few troubleshooting tips if things go sideways:
- Double-check your file path: It’s easy to accidentally point to the wrong directory or a corrupted copy of the file.
- Confirm the file size hasn’t changed: Use
os.path.getsize(raw_file_path)to ensure it’s still175759360bytes. - If your data looks garbled after reading, try adjusting the reshape order or transposing: Sometimes raw files might be stored in a different implicit order. For example, you could try reshaping to
(224, 640, 613)withorder='C'then transposing to(613, 640, 224)—though the first method should work if your original successful runs used Fortran order.
Let me know if you hit any specific snags—like weird data values or shape errors—and we can troubleshoot further!
内容的提问来源于stack exchange,提问作者Urban Avsec

