以前用的是这套接口,总感觉慢腾腾的,像老大爷散步
def bin2numpy(file_path, shape):
pic_gray = np.zeros(shape)
with open(file_path, "rb") as f:
for i in range(shape[0]):
for j in range(shape[1]):
data = f.read(2)
pix = struct.unpack('H', data)[0]
pic_gray[i, j] = pix
return pic_gray
写个测试程序猛然发现读一张1920x1080的raw格式图片要nm
一秒钟。。。
一秒。。
一。
1.019157886505127
果断换用np.fromfile接口
def bin2numpy_ex(file_path, shape):
rawImg = np.fromfile(file_path, dtype=np.uint16)
rawImg = rawImg[: shape[0]*shape[1]]
pic_gray = rawImg.reshape(shape)
return pic_gray
再次测试
0.0018076896667480469
真香