【问题标题】:How to convert np array in a specific pattern and save it to the binary file?如何以特定模式转换 np 数组并将其保存到二进制文件中?
【发布时间】:2021-02-26 01:11:43
【问题描述】:

我确实有一个二进制文件(原始图像),我用特定模式读取它,如下代码,我现在想处理 np 数组(new_array),所以在处理完new_array 之后,怎么做我将其转换回数组并将其保存到新的二进制文件(原始图像)?

f = np.fromfile(path, dtype=np.uint16)

new_array = np.zeros(cols * rows, dtype=int)
for i in range(0, cols * rows):
    tmp = '{0:08b}'.format(f[i])
    part1 = tmp[4:8]
    part2 = tmp[0:4]
    new_array[i] = (int(part1,2)<<2) + (int(part2,2)>>6)

res_array = new_array.reshape((rows, cols))

上面给出了模式。 我只是想知道如何将res_array 转换为原始类型(变量f)并将其保存到新的二进制文件中。

【问题讨论】:

    标签: python numpy binaryfiles


    【解决方案1】:

    我想你可以flatten the np array (res_array) 写出结果np array to a binary file

    展平数组会将其“转换回”np.uint16 一维数组。然后,编写二进制文件应该转储原始结果而不对其进行编码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-11
      • 1970-01-01
      • 2016-08-01
      • 1970-01-01
      • 2016-10-21
      相关资源
      最近更新 更多