Common pydicom functions called by user code
The main function to read and parse DICOM files using pydicom is read_file. It is coded in the module dicom.filereader, but is also imported when the pydicom package is imported:
>>> import pydicom
>>> dataset = pydicom.read_file(...)
If you need fine control over the reading, you can either call read_partial or use open_dicom. All are documented below:
Read and parse a DICOM file.
Read file and return file dataset: >>> rtplan = pydicom.read_file(“rtplan.dcm”) >>> rtplan.PatientName
Use within a context manager: >>> with pydicom.read_file(“rtplan.dcm”) as rtplan: >>> rtplan.PatientName
Parse a DICOM file until a condition is met.
Use read_file unless you need to stop on some condition other than reaching pixel data.
FileDataset instance or DicomDir instance.
DICOM files can also be written using pydicom. There are two ways to do this. The first is to use write_file with a prexisting FileDataset (derived from Dataset) instance. The second is to use the save_as method on an Dataset instance.
Store a FileDataset to the filename specified.
If True (default), preserves the following information from the dataset: -preamble – if no preamble in read file, than not used here -hasFileMeta – if writer did not do file meta information,
then don’t write here either
sequence, write the undefined length delimiters if that is what the original had.
Set dataset.preamble if you want something other than 128 0-bytes. If the dataset was read from an existing dicom file, then its preamble was stored at read time. It is up to the user to ensure the preamble is still correct for its purposes.
If there is no Transfer Syntax tag in the dataset, then set dataset.is_implicit_VR and dataset.is_little_endian to determine the transfer syntax used to write the file.
Write the dataset to a file.
If True (default), preserves the following information from the dataset: -preamble – if no preamble in read file, than not used here -hasFileMeta – if writer did not do file meta information,
then don’t write here either
sequence, write the undefined length delimiters if that is what the original had.
Set dataset.preamble if you want something other than 128 0-bytes. If the dataset was read from an existing dicom file, then its preamble was stored at read time. It is up to the user to ensure the preamble is still correct for its purposes.
If there is no Transfer Syntax tag in the dataset, then set dataset.is_implicit_VR and dataset.is_little_endian to determine the transfer syntax used to write the file.
A collection (dictionary) of Dicom DataElement instances.
Example of two ways to retrieve or set values:
Example (2) uses DICOM “keywords”, defined starting in 2011 standard. PatientName is not actually a member of the object, but unknown member requests are checked against the DICOM dictionary. If the name matches a DicomDictionary descriptive string, the corresponding tag is used to look up or set the DataElement instance’s value.
Attribute indent_chars: | |
---|---|
for string display, the characters used to indent nested Data Elements (e.g. sequence items). Default is three spaces. |