[英]convert raw binary 8 bit unsigned file to 16 bit unsigned with the python imaging library
[英]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.