Hey everyone, I’m stuck on a problem with saving CFA sensor data as a raw image file. I want to use PIL to save the 8-bit per pixel CFA buffer as a raw file that I can later process with LibRaw or dcraw.
I’ve tried saving it as a .raw or .tiff file using PIL, but I can’t load it afterward. When I use numpy’s imread, it looks like each pixel has the same value for r, g, and b.
Here’s what I’ve tried so far:
def save_cfa_data(input_data):
with open(input_data, 'rb') as f:
data = f.read()
processed_data = bytearray(data)
with open('output.raw', 'wb') as f:
f.write(processed_data)
img = Image.frombuffer('P', (4096, 3072), processed_data, 'raw', 'P', 0, 1)
img.convert('L').save('grayscale_output.tiff')
Any ideas on how to correctly save this CFA data as a raw image file? Thanks in advance for your help!
I’ve encountered a similar situation with CFA sensor data, and the trick is often in how the raw bytes are reshaped and processed. Since each pixel in CFA data only records one color, you need to create a 2D array that matches your sensor’s dimensions.
One effective approach is to use numpy to read and reshape the data, then save it as a 16-bit TIFF file. This format can retain the full data for processing tools like LibRaw or dcraw.
import numpy as np
from PIL import Image
def save_cfa_data(input_data, width, height):
with open(input_data, 'rb') as f:
data = np.fromfile(f, dtype=np.uint8)
reshaped_data = data.reshape((height, width))
img = Image.fromarray(reshaped_data, mode='I;16')
img.save('output.tiff', format='TIFF')