pydicom.filereader.dcmread

pydicom.filereader.dcmread(fp: str | bytes | PathLike | BinaryIO | DicomFileLike, defer_size: int | float | str | None = None, stop_before_pixels: bool = False, force: bool = False, specific_tags: List[int] | List[str] | List[Tuple[int, int]] | List[BaseTag] | None = None) FileDataset | 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.

Deprecated since version 2.2: Returning a DicomDir is deprecated and will be removed in v3.0. Use FileSet instead.

Examples

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

>>> ds = pydicom.dcmread("CT_small.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
Parameters:
  • fp (str or PathLike or file-like) – Either a file-like object, a string containing the file name or the path to the file. The file-like object must have seek(), read() and tell() methods and the caller is responsible for closing it (if required).

  • defer_size (int, str or float, optional) – If not used then 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. Should be the number of bytes to be read as int or as a str 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 used the only the supplied tags will be returned. The supplied elements can be tags or keywords. 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 False 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.