pythoncsvpydicom

write dicom header to csv


I've got a bunch of .dcm-files (dice-files) where I would like to extract the header and save the information there in a CSV file.

As you can see in the following picture, I've got a problem with the delimiters:

part of dicom-header

For example when looking at the second line in the picture: I'd like to split it like this:

0002 | 0000 | File Meta Information Group Length | UL | 174

But as you can see, I've not only multiple delimiters but also sometimes ' ' is one and sometimes not. Also the length of the 3rd column varies, so sometimes there is only a shorter text there, e.g. Image Type further down in the picture.

Does anyone have a clever idea, how to write it in a CSV file?
I use pydicom to read and display the files in my IDE. I'd be very thankful for any advice :)


Solution

  • I would suggest going back to the data elements themselves and working from there, rather than from a string output (which is really meant for exploring in interactive sessions)

    The following code should work for a dataset with no Sequences, would need some modification to work with sequences:

    import csv
    import pydicom
    from pydicom.data import get_testdata_file
    
    filename = get_testdata_file("CT_small.dcm")  # substute your own filename here
    ds =  pydicom.dcmread(filename)
    
    with open('my.csv', 'w', newline='') as csvfile:
        writer = csv.writer(csvfile)
        writer.writerow("Group Elem Description VR value".split())
        for elem in ds:
            writer.writerow([
                f"{elem.tag.group:04X}", f"{elem.tag.element:04X}",
                elem.description(), elem.VR, str(elem.value)
            ])
    

    It may also require a bit of change to make the elem.value part look how you want it, or you may want to set the CSV writer to use quotes around items, etc.

    Output looks like:

    Group,Elem,Description,VR,value
    0008,0005,Specific Character Set,CS,ISO_IR 100
    0008,0008,Image Type,CS,"['ORIGINAL', 'PRIMARY', 'AXIAL']"
    0008,0012,Instance Creation Date,DA,20040119
    0008,0013,Instance Creation Time,TM,072731
    ...