繁体   English   中英

Python读取无符号16位整数的二进制文件

[英]Python read binary file of unsigned 16 bit integers

我必须在Python中读取二进制文件,并将其内容存储在数组中。 我在此文件上的信息是

filename.bin is of size 560x576 (height x width) with 16 b/p, i.e., unsigned 16-bit integer for each pixel

到目前为止,这是我能想到的:

import struct
import numpy as np
fileName = "filename.bin"

with open(fileName, mode='rb') as file: 
    fileContent = file.read()



a = struct.unpack("I" * ((len(fileContent)) // 4), fileContent)

a = np.reshape(a, (560,576))

但是我得到了错误

cannot reshape array of size 161280 into shape (560,576)

161280恰好是560 x 576 = 322560 我想了解我在做什么错以及如何读取二进制文件并以所需的形式进行整形。

您使用的是32位无符号格式的“ I”,而不是16位无符号格式的“ H”。

做这个

a = struct.unpack("H" * ((len(fileContent)) // 2), fileContent)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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