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

  • I have a Dicom Image and I did read it with pydicom.dcmread('1.dcm').
  • how can I write just the metadata, without the pixel_array? either as dictionary, or as dicom format.
  • I tried to do it with the following piece of code, but it didn't work!
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 1:[1]

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.

Solution 2:[2]

the easiest way:

from pydicom import dcmread
meta = dcmread('mri.dcm', specific_tags=(0,0))
meta

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 stansy