python - 使用 NumPy 将二进制数据读取为 BGRA 图像

标签 python numpy image-formats

我将 BGRA 图像转储为以下格式(连续)的二进制文件:[(b, g, r, a), (b, g, r, a), (b, g, r, a ) ...] 我知道图像的宽度、高度和 channel 数量(在这种情况下当然是 4 个)。

我想将图像读入一个 4D 数组,其中第一个维度表示 B 值等。 我有以下代码:

data = np.fromfile(fbin, np.dtype('B'))
print data

这会打印出如下内容:

[ 79  90  92   0  80  91  93   0  84  96  98   0 ...]

其中 79 是 B,90 是 G,92 是 R,0 是 A,依此类推。现在我尝试像这样 reshape “数据”:

print data.reshape(channels, height, width)

得到以下内容:

[[[ 79  90  92   0  ...] .. [] ..]

  [[109 ...] .. [] ..]

  [[118 ...] .. [] ..]

  [[  0 ...] .. [] ..]]

我想得到的是这样的东西:

[[[ 79 ...] .. [] ..]

  [[90 ...] .. [] ..]

  [[92...] .. [] ..]

  [[0...] .. [] ..]]

最佳答案

好吧,这感觉太简单了,解决方案是:

data = data.reshape(channels, width, height, order='F')

关于python - 使用 NumPy 将二进制数据读取为 BGRA 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28278075/

相关文章:

python - 访问 token 和刷新 token 在 Python 的 Google Plus 中提供无效授权?

python - 格式化字符串并将其传递给 python 中的 URL

python - 使用 numpy.logspace() 时出错 : how to generate numbers spaced evenly on a log-scale

python - 用一行在python中获取数组元素

c++ - OpenCV imencode pgm 没有编码正确的格式

Python- 10 次方的数字的扩展形式

python - 如何卸载使用 distutils 安装的 python 包?

python - 尝试绘制一个简单的函数 - python

cross-browser - 浏览器支持哪些图形文件格式?

Android截图错误: RGBA override BLOB format buffer should have height == width