pydicom.filereader.dcmread

pydicom.filereader.dcmread(fp: Union[str, os.PathLike[AnyStr], BinaryIO], defer_size: Optional[Union[str, int]] = None, stop_before_pixels: bool = False, force: bool = False, specific_tags: Optional[List[Union[int, str, Tuple[int]]]] = None) → Union[pydicom.dataset.FileDataset, pydicom.dicomdir.DicomDir][source]

Read and parse a DICOM dataset stored in the DICOM File Format.

Read a DICOM dataset stored in accordance with the DICOM File Format. 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 PathLike 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.

  • defer_size (int or str or None, optional) – If None (default), all elements are read into memory. If specified, then if a data element’s stored value is larger than defer_size, 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”.

  • stop_before_pixels (bool, optional) – 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, optional) – 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.

  • specific_tags (list of (int or str or 2-tuple of int), optional) – If not None, only the tags in the list are returned. The list elements can be tags or tag names. Note that the element (0008,0005) Specific Character Set is always returned if present - this ensures correct decoding of returned text values.

Returns

An instance of FileDataset that represents a parsed DICOM file, unless the dataset is a Media Storage Directory instance in which case it will be a DicomDir.

Return type

FileDataset or DicomDir

Raises
  • InvalidDicomError – If force is True and the file is not a valid DICOM file.

  • TypeError – If fp is None or of an unsupported type.

See also

pydicom.dataset.FileDataset

Data class that is returned.

pydicom.filereader.read_partial

Only read part of a DICOM file, stopping on given conditions.

Examples

Read and return a dataset stored in accordance with the DICOM File Format:

>>> ds = pydicom.dcmread("rtplan.dcm")
>>> ds.PatientName

Read and return a dataset not in accordance with the DICOM File Format:

>>> ds = pydicom.dcmread("rtplan.dcm", force=True)
>>> ds.PatientName

Use within a context manager:

>>> with pydicom.dcmread("rtplan.dcm") as ds:
>>>     ds.PatientName