open .raw image data using python

心已入冬 提交于 2020-06-10 09:59:20

问题


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.

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.

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

then an error occurs with

ERROR: Image data can not convert to float

Any idea will be appreciated.

link: camera module

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

回答1:


Have a look at rawpy:

import rawpy
import imageio

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

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

See also the API docs.



来源:https://stackoverflow.com/questions/32439831/open-raw-image-data-using-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!