pydicom.filereader.read_preamble

pydicom.filereader.read_preamble(fp: BinaryIO, force: bool) bytes | None[source]

Return the 128-byte DICOM preamble in fp if present.

fp should be positioned at the start of the file-like. If the preamble and prefix are found then after reading fp will be positioned at the first byte after the prefix (byte offset 133). If either the preamble or prefix are missing and force is True then after reading fp will be positioned at the start of the file-like.

Parameters:
  • fp (file-like object) – The file-like to read the preamble from.

  • force (bool) – Flag to force reading of a file even if no header is found.

Returns:

preamble – The 128-byte DICOM preamble will be returned if the appropriate prefix (‘DICM’) is found at byte offset 128. Returns None if the ‘DICM’ prefix is not found and force is True.

Return type:

bytes or None

Raises:

InvalidDicomError – If force is False and no appropriate header information found.

Notes

Also reads past the ‘DICM’ marker. Rewinds file to the beginning if no header found.