Add new comment

Hi Alex,

Hi Alex,
I am using python ctypes to call the c api to libraw. With D5200 and D7100 nef files it works, but with D4, D700, D800 etc it fails. I noted that D700 is actually a smaller image[] array than D7100.

Here is my code: (I did not include the massive struct declarations from the top of the source code for brevity).

from ctypes import *
from ctypes.util import find_library
import numpy as np
from PIL import image
 
lr=CDLL('libraw')
 
#read test file into buf
buf=create_string_buffer(open('colortest.nef', 'rb').read())
 
#make it so init returns a pointer to struct declared above
lr.libraw_init.restype = POINTER(libraw_data_t)
 
handle = lr.libraw_init(0)
lr.libraw_open_buffer(handle, buf, len(buf))
lr.libraw_unpack(handle)
lr.libraw_raw2image(handle)
 
rwidth=handle.contents.sizes.raw_width
rheight=handle.contents.sizes.raw_height
size=4*rwidth*rheight*2 #size of image[] , 2 is np.uint16 size
 
#open handle.contents.image as a numpy array of unsigned 16bit integers
buffer_from_memory = pythonapi.PyBuffer_FromMemory
buffer_from_memory.restype = py_object
buffer = buffer_from_memory(handle.contents.image, size)
a= np.frombuffer(buffer, np.uint16)
 
#reshape from 1d to 2d array
a.shape=(rwidth*rheight,4)
 
#grab only the red pixel array and store it in rs
rs = a[:,0].reshape(rheight,rwidth)
 
#save rs as 16bit tiff
im = Image.fromarray(rs, mode='I;16')
im.save('testred.tif')

So if I run this code with my NEF files, as long as the nef file has d7100 in the exif data, it will save a tiff. If it finds, for example, D800 I will get a seg fault which mentions memcpy() . Because I am using python with ctypes I do not get much more information than that.

I tested several of the included executable in libraw and they all work for all of my nef files.