numpy - 使用 python 打开 .raw 图像数据

标签 numpy python-imaging-library

我一直在谷歌搜索使用 python 库显示原始图像数据的方法,但找不到任何合适的解决方案。数据取自相机模块,扩展名为“.raw”。此外,当我尝试通过“更多文件名.raw”在终端中打开它时,控制台说这是一个二进制文件。供应商告诉我,相机输出 16 位原始灰度数据。

但我想知道如何通过 PIL、Pillow 或 Numpy 显示这些数据。我已经测试了 PIL 的 Image 模块。但是,它无法识别图像数据文件。 PIL 似乎没有将 .raw 文件视为图像数据格式。可以显示 BMP 文件,但不能显示此“.raw”。

另外,当我尝试使用 read 函数和 matplotlib 时,如下所示

from matplotlib import pyplot as plt
f = open("filename.raw", "rb").read() 
plt.imshow(f) 
plt.show()

然后发生错误

ERROR: Image data can not convert to float



任何想法将不胜感激。

关联:
camera module

我用以下代码做了一些改进。但现在的问题是这段代码只显示了整个图像的一部分。
from matplotlib import pyplot as plt
import numpy as np
from StringIO import StringIO
from PIL import *
scene_infile = open('G0_E3.raw','rb')
scene_image_array = np.fromfile(scene_infile,dtype=np.uint8,count=1280*720)
scene_image = Image.frombuffer("I",[1280,720],
                                 scene_image_array.astype('I'),
                                 'raw','I',0,1)
plt.imshow(scene_image)
plt.show()

最佳答案

import numpy as np
from PIL import Image
import rawpy


# For Adobe DNG image
input_file = 'path/to/rawimage.dng'
rawimg = rawpy.imread(input_file)
npimg = rawimg.raw_image


# For raw image (without any headers)
input_file = 'path/to/rawimage.raw'
npimg = np.fromfile(input_file, dtype=np.uint16)
imageSize = (3648, 2736)
npimg = npimg.reshape(imageSize)


# Save the image from array to file.
# TIFF is more suitable for 4 channel 10bit image
# comparing to JPEG
output_file = 'out.tiff'
Image.fromarray(npimg/1023.0).save(output_file)

关于numpy - 使用 python 打开 .raw 图像数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32439831/

相关文章:

Python:计算具有重复值的列中每个唯一值的第一个实例

python - 使用 PIL 渲染表情符号

python - 在numpy数组中查找最大元素的索引,不包括少数索引

python - 乘法适用于密集矩阵,但不适用于压缩行矩阵

python - 使用 numpy.savetxt 和 numpy.loadtxt 写入和读取复数

python - numpy中的分层抽样

python - 有没有办法像处理打开的文件一样处理 PIL-Image?

python - 如何在不保存的情况下将Image PIL转成Base64

Python PIL 以日期时间作为名称保存文件

python - 用 Pillow 保存在内存文件对象中