Jul 11, 2013 - I get asked, and am often in need myself, of sample DICOM files in various formats. By sample I mean no weird private tags and all of the.
import pydicom dataset = pydicom. ) If you need fine control over the reading, you can either call readpartial or use opendicom. All are documented below: pydicom.filereader.
Dcmread ( fp, defersize=None, stopbeforepixels=False, force=False, specifictags=None ) Read and parse a DICOM dataset stored in the DICOM File Format. Read a DICOM dataset stored in accordance with the DICOM File Format (DICOM Standard Part 10 Section 7). If the dataset is not stored in accordance with the File Format (i.e. The preamble and prefix are missing, there are missing required Type 1 File Meta Information Group elements or the entire File Meta Information is missing) then you will have to set force to True. Parameters: fp: str or file-like Either a file-like object, or a string containing the file name.
If a file-like object, the caller is responsible for closing it. Defersize: int or str or None If None (default), all elements read into memory.
If specified, then if a data element’s stored value is larger than defersize, the value is not read into memory until it is accessed in code. Specify an integer (bytes), or a string value with units, e.g. “512 KB”, “2 MB”.
Stopbeforepixels: bool If False (default), the full file will be read and parsed. Set True to stop before reading (7FE0,0010) ‘Pixel Data’ (and all subsequent elements). Force: bool If False (default), raises an InvalidDicomError if the file is missing the File Meta Information header. Set to True to force reading even if no File Meta Information header is found. Specifictags: list or None If not None, only the tags in the list are returned.
The list elements can be tags or tag names. Note that the tag Specific Character Set is always returned if present - this ensures correct decoding of returned text values. Returns: FileDataset An instance of FileDataset that represents a parsed DICOM file. Raises: InvalidDicomError If force is True and the file is not a valid DICOM file.
with pydicom. Dcmread ( 'rtplan.dcm' ) as ds: ds.
PatientName pydicom.filereader. Readpartial ( fileobj, stopwhen=None, defersize=None, force=False, specifictags=None ) Parse a DICOM file until a condition is met. Parameters: fileobj: a file-like object Note that the file will not close when the function returns. Stopwhen: Stop condition. See readdataset for more info. Defersize: int, str, None, optional See dcmread for parameter info. Force: boolean See dcmread for parameter info.
Specifictags: list or None See dcmread for parameter info. Returns: FileDataset instance or DicomDir instance.
File Writing DICOM files can also be written using pydicom. There are two ways to do this. The first is to use writefile with a prexisting FileDataset (derived from Dataset) instance. The second is to use the saveas method on an Dataset instance. Writefile ( filename, dataset, writelikeoriginal=True ) Write dataset to the filename specified. If writelikeoriginal is True then dataset will be written as is (after minimal validation checking) and may or may not contain all or parts of the File Meta Information (and hence may or may not be conformant with the DICOM File Format).
If writelikeoriginal is False, dataset will be stored in the DICOM File Format in accordance with DICOM Standard Part 10 Section 7. The byte stream of the dataset will be placed into the file after the DICOM File Meta Information. Parameters: filename: str or file-like Name of file or the file-like to write the new DICOM file to.
Dataset: pydicom.dataset.FileDataset Dataset holding the DICOM information; e.g. An object read with pydicom.dcmread.
Writelikeoriginal: bool If True (default), preserves the following information from the Dataset (and may result in a non-conformant file): - preamble – if the original file has no preamble then none will be. filemeta – if the original file was missing any required File Meta Information Group elements then they will not be added or written. If (0002,0000) ‘File Meta Information Group Length’ is present then it may have its value updated.
seq.isundefinedlength – if original had delimiters, write them now too, instead of the more sensible length characters. isundefinedlengthsequenceitem – for datasets that belong to a sequence, write the undefined length delimiters if that is what the original had. If False, produces a file conformant with the DICOM File Format, with explicit lengths for all elements.
See also Dataset class with relevant attributes and information. Write a DICOM file from a dataset that was read in with dcmread.
Saveas wraps dcmwrite. Saveas ( filename, writelikeoriginal=True ) Write the Dataset to filename. Saving a Dataset requires that the Dataset.isimplicitVR and Dataset.islittleendian attributes exist and are set appropriately.
If Dataset.filemeta.TransferSyntaxUID is present then it should be set to a consistent value to ensure conformance. Parameters: filename: str or file-like Name of file or the file-like to write the new DICOM file to. Writelikeoriginal: bool If True (default), preserves the following information from the Dataset (and may result in a non-conformant file): - preamble – if the original file has no preamble then none will. filemeta – if the original file was missing any required File Meta Information Group elements then they will not be added or written. If (0002,0000) ‘File Meta Information Group Length’ is present then it may have its value updated. seq.isundefinedlength – if original had delimiters, write them now too, instead of the more sensible length characters. isundefinedlengthsequenceitem – for datasets that belong to a sequence, write the undefined length delimiters if that is what the original had.
If False, produces a file conformant with the DICOM File Format, with explicit lengths for all elements.