Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

open .raw image data using python

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()
like image 314
Nownuri Avatar asked Sep 07 '15 13:09

Nownuri


People also ask

How can I view raw data as an image?

To open a RAW file, you need image editing software such as Adobe Photoshop or Adobe Lightroom. The most appropriate software to open a RAW file depends on your camera type and computer operating system or smartphone. After opening a RAW file, you can then convert and export it in your desired image format.

How do I open a RAW file in Python?

Reading in raw files with Python We use the open() command to read the file. We first pass the name of the file we want to process and then an r which stands for read.

How do I read an image from a file in Python?

Read An image We use cv2. imread() function to read an image. The image should be placed in the current working directory or else we need to provide the absoluate path.


2 Answers

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)
like image 129
Nice Avatar answered Oct 19 '22 03:10

Nice


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.

like image 16
letmaik Avatar answered Oct 19 '22 04:10

letmaik