Applying .dcmread()
wraps a DataSet, a dictionary data structure {}. This DataSet contains keys and values represented like the following:
- Keys: contains the DICOM Tags of the attributes specified in the DICOM file you are reading. Examples of the keys like:
- (0x0010, 0x0010) PatientName attribute.
- (0x0028, 0x0010) Rows attribute.
- (0x7fe0, 0x0010) PixelData attribute.
- The tags’ numbers consist of two hexadecimal, the first refers to the group, and the second refers to a specific element. So, you might find many attributes that have the same first number of tags.
- Values: the values of this dictionary generally contain the following:
- Tag: the element’s tag like (0028, 0030), for example.
- Keyword: describes what the attribute refers to. The keyword of the tag (0028, 0030) is “Pixel Spacing”.
- VR: it’s only two characters that refer to the Value Representation of the element, which describes the data type and format of the attribute value. The VR of the tag (0028, 0030) is “DS”, Decimal String. You can see the VR of any tag and how it is represented using Python structures following the link.
- Value: the actual value of the element. It could be an integer, a string, a list, or even a Sequence, which is a dataset of attributes. The value of the tag (0028, 0030) is a list of two floats that represent the physical distance along the rows and columns, respectively, in mm.
PyDicom DataSet
A DICOM DataSet is a mutable mapping of DICOM DataElements. Each DataElement, a value of the dictionary, in the DICOM DataSet has a unique tag, a key of the dictionary, that identifies it. For example, the “PatientName” attribute corresponds to the tag (0x0010, 0x0010) in the DICOM standard, which identifies the patient’s name data element.
Example
# Import pydicom
import pydicom
# read a DICOM file
dcm = pydicom.dcmread('test.dcm')
# print the entire DICOM header
print(dcm)
# Extract the patient's name.
patient_name = dcm.PatientName
print(patient_name)
# Extract the patient's name using its unique DICOM tag (0010, 0010)
patient_value = dcm[0x0010, 0x0010]
print(patient_value)