Search code examples
pythondicompydicom

How to access all tags from a DICOM group number?


I would like to extract from a DICOM all the dicom tags with the same group number. For example, I would like to extract all tags belonging to the group 0010. I tried this, and of course it made an error:

import pydicom
ds=pydicom.dcmread('dicomfile.dcm')

print(ds[0x0008,0x0020]) # print the tag corresponding to group number 0008, and element number 0020

print(ds[0x0010,:]) # ERROR, does not print a list of all tags belonging to group 0008.

Is there a pydicom method to do it?


Solution

  • As @mrbean-bremen and @darcymason have said, there's two ways to get a range of elements via their tag values. You can return an arbitrary range of elements using slicing:

    import pydicom
    
    ds = pydicom.dcmread("path/to/file")
    # Must use the full group + element tag
    print(ds[0x00100000:0x00110000])
    

    Or if you just need a particular group then you can use Dataset.group_dataset():

    import pydicom
    
    ds = pydicom.dcmread("path/to/file")
    # Just need the element group number
    print(ds.group_dataset(0x0010))
    

    Both methods will return a new Dataset instance containing the elements.