pythonpydicom

How can I save the metadata only of a dicom image, without the PixelArray?


import os
import pydicom
path='dataset'
dico = pydicom.dcmread('1.dcm')
dico.pixel_array = None
dico.save_as(os.path.join(path,'Metadata.dcm'))

Solution

  • If you don't need the pixel data at all, you can use:

    dico = pydicom.dcmread('1.dcm', stop_before_pixels=True)
    

    In this case only the tags before the pixel data, e.g. the header data are read (note that in rare cases some private data can exist after the pixel data, but this can usually be ignored).

    If you want to remove the pixel data after reading, you have to remove the PixelData tag:

     dico = pydicom.dcmread('1.dcm')
     del dico.PixelData
     dico.save_as(os.path.join(path,'Metadata.dcm'))
    

    Note that pixel_data is created from the PixelData tag on demand - while PixelData is in raw format (depending on Endianess and possible compression), pixel_data is a NumPy array in a format that can be used for image processing. Removing it does not remove the original pixel data.