使用 python 打开 .raw 图像数据 [英] open .raw image data using python

查看:605
本文介绍了使用 python 打开 .raw 图像数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have been searching google for the method to display a raw image data using python libraries but couldn't find any proper solution. The data is taken from a camera module and it has the '.raw' extension. Also when I tried to open it in the terminal via 'more filename.raw', the console said that this is a binary file. Vendor told me that the camera outputs 16-bits raw greyscale data.

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

But I wonder how I can display this data via PIL, Pillow or just Numpy. I have tested the PIL's Image module. However, it couldn't identify the image data file. It seems the PIL doesn't consider the .raw file as an image data format. BMP files could be displayed, but this '.raw' couldn't.

当我尝试使用只读函数和 matplotlib 时,如下所示

Also when I tried with just read function and matplotlib, like the followings

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

任何想法将不胜感激.

链接:相机模块

我使用以下代码进行了一些改进.但现在的问题是这段代码只显示了整个图像的一部分.

I made some improvement with the following codes. But now the issue is that this code displays only some portion of the entire image.

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()

推荐答案

看看 rawpy:

import rawpy
import imageio

path = 'image.raw'
raw = rawpy.imread(path)
rgb = raw.postprocess()
imageio.imsave('default.tiff', rgb)

rgb 只是一个 RGB numpy 数组,因此您可以使用任何库(不仅仅是 imageio)将其保存到磁盘.

rgb is just an RGB numpy array, so you can use any library (not just imageio) to save it to disk.

如果您想访问未处理的拜耳数据,请执行以下操作:

If you want to access the unprocessed Bayer data, then do:

bayer = raw.raw_image

另请参阅 API 文档.

这篇关于使用 python 打开 .raw 图像数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆