pydicom.dcmread() consumes a lot of memory when working with bigger files

75 Views Asked by At

Is there a way where we can read the dicom file using buffer?

I tried defer_size = '50 MB' , but when i accessed pixel data, all the pixel data was read and stored in memory

Can I do something like:

def buffered_dcmread(file_path, buffer_size=4096):
    with open(file_path, 'rb') as file:
        buffer = file.read(buffer_size)
        while buffer:
            dataset = pydicom.dcmread(buffer, stop_before_pixels=True
            file.seek(dataset.end_tell)
            buffer = file.read(buffer_size)
 

dicom_file_path = 'dicom_file.dcm'

buffered_dcmread(dicom_file_path)

I am working with multi-frame files and I need to convert each frame to a jpeg

How do I do that without loading all the pixel data into the memory?

1

There are 1 best solutions below

3
darcymason On

I don't think your code pattern above would be very helpful, because internally pydicom will still probably try to pull everything into memory.

There has been a lot of development in pydicom lately for using buffered objects and reading pixels by frame (e.g. #1895). These are still in the development code, not yet released, but will probably soon be stable enough if you want to try them out.

Alternatively, if you want to use stable pydicom, you can try to dcmread an mmap file, e.g.:

from mmap import mmap, ACCESS_READ
from pydicom.data import get_testdata_file
from pydicom import dcmread

f = open(get_testdata_file(filename), 'rb')
mm = mmap(f.fileno(), 0, access=ACCESS_READ)
ds = dcmread(mm)

But pydicom may still try to read large values into internal memory, so that may not be much help depending on the files and how pixel data is manipulated.