我一直在谷歌搜索使用 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/