0
我在阅读python中的二进制文件并绘制它时遇到了问题。它应该是一个代表1000x1000整数数组的无格式二进制文件。我已经使用:将二进制文件读入二维数组python
image = open("file.dat", "r")
a = np.fromfile(image, dtype=np.uint32)
打印的长度返回500000.我想不出如何创建一个二维数组。
我在阅读python中的二进制文件并绘制它时遇到了问题。它应该是一个代表1000x1000整数数组的无格式二进制文件。我已经使用:将二进制文件读入二维数组python
image = open("file.dat", "r")
a = np.fromfile(image, dtype=np.uint32)
打印的长度返回500000.我想不出如何创建一个二维数组。
由于您使用
a = np.fromfile(image, dtype=np.uint32)
,那么你将使用
a = np.fromfile(image, dtype=np.uint16)
还有其他的可能性得到一百万uint16
S,然而得到一百万uint32
同父异母。在D型可以是任何一个16位整数D型如
>i2
(big-endian的16位有符号整数),或<i2
(little-endian的16位有符号整数),或<u2
(小端16位无符号整数)或>u2
(big-endian 16位无符号整数)。np.uint16
与<u2
或>u2
相同,具体取决于机器的字节顺序。
例如,
import numpy as np
arr = np.random.randint(np.iinfo(np.uint16).max, size=(1000,1000)).astype(np.uint16)
arr.tofile('/tmp/test')
arr2 = np.fromfile('/tmp/test', dtype=np.uint32)
print(arr2.shape)
# (500000,)
arr3 = np.fromfile('/tmp/test', dtype=np.uint16)
print(arr3.shape)
# (1000000,)
然后得到的形状(1000,1000),使用重塑的数组:
arr = arr.reshape(1000, 1000)
如果是二进制的,你应该用'RB打开' – 2015-03-31 23:42:11