Open .raw image data with python

I searched google for a method to display raw image data using python libraries, but did not find a suitable solution. Data is taken from the camera module and has the extension ".raw". Also, when I tried to open it in the terminal via "more filename.raw", the console reported that it was a binary file. The vendor told me that the camera is outputting 16-bit raw gray data.

But I am wondering how I can display this data through PIL, Pillow or just Numpy. I tested the PIL image module. However, he was unable to identify the image data file. PIL does not seem to consider the .raw file as an image data format. BMP files may be displayed, but this ".raw" could not.

Also, when I tried using the read function and matplotlib, for example, the following

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 cannot be converted to float

Any idea would be appreciated.

reference: camera module

I made some improvements with the following codes. But now the problem is that this code only displays part of the 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()
+4
source share
1 answer

Take a look at rawpy :

import rawpy
import imageio

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

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

If you want to access raw Bayer data, follow these steps:

bayer = raw.raw_image

. API docs.

+3

Source: https://habr.com/ru/post/1606266/


All Articles