Skip to main content

dicom_header

Single-open DICOM header access and metadata-only frame counting.

Reading a DICOM over a network share is dominated by round trips, not by bytes transferred: every open() is an SMB2 CREATE and every read is a round trip. This module opens a file once, reads a small first chunk that holds most headers whole, and exposes the parsed dataset plus the bytes at the start of the Pixel Data element, so callers can re-parse as often as they like without touching the share again.

Heidelberg's HEYEX export embeds E2E records in private elements ahead of the Pixel Data element, and one of them repeats the images, so together they can be most of the file. Only the start of a record is ever used, so the read keeps that and seeks past the rest: the bodies of the records never cross the share.

It also derives the frame count from element metadata rather than by decoding pixel data. NumberOfFrames is absent from most single-frame images and from non-image DICOMs, and some converted files omit it while genuinely holding multiple frames, so a frame count has to come from somewhere; taking it from the Pixel Data length costs twelve bytes instead of a full transfer and decode.

Module​

Functions​

frames_from_header​

def frames_from_header(header: DicomHeaderRead) ‑> int | None:

Derive the frame count from element metadata, without decoding pixels.

Tries, in order: the Number of Frames tag; the Pixel Data length divided by the size of one frame (exact for uncompressed data); the Extended Offset Table; the Basic Offset Table.

Arguments

  • header: A header read by read_dicom_header.

Returns The frame count, or None if it cannot be determined from metadata alone — in which case the caller must fall back to reading pixel data.

media_storage_sop_class_name​

def media_storage_sop_class_name(ds: pydicom.Dataset) ‑> str:

The Media Storage SOP Class UID's name, from an already-parsed dataset.

read_dicom_header​

def read_dicom_header(    location: FileLocation, *, head_bytes: int = 8192,) ‑> DicomHeaderRead:

Read a DICOM file's header in a single open.

Reads head_bytes and parses from memory. An embedded E2E record is cut to its prefix: the read seeks past the rest and reads head_bytes more from where it ends. If the parse otherwise runs out of bytes before reaching the Pixel Data element, the read grows from the same handle: to where the element it ran out in ends, when that is declared, or else to twice what is held. A header that would grow past GROW_LIMIT_BYTES, or that ran out inside Zeiss CZM data (group 0x0407), has the rest of the file read instead: an extra transfer, but not an extra open. Once the Pixel Data element is reached, the read takes head_bytes more if the Basic Offset Table's item header after it is not yet held, and never the rest of the file.

The file's size is never queried: a read returning fewer bytes than asked for has reached the end, which answers the only question size would. Over SMB a stat of an open handle is itself a round trip, which is the cost this function exists to avoid.

Arguments

  • location: The file's on-disk spelling. A path key is located first (fs_utils.locate), since a folded key may not open.
  • head_bytes: How much to read on the first attempt, after each record skipped, and past the end of an element the parse ran out in.

Returns The parsed header and the bytes it came from.

Raises

  • FileNotFoundError: If the file is missing. Callers are expected to handle this, since the file may reappear on a later run.

Classes​

DicomHeaderRead​

class DicomHeaderRead(    ds: pydicom.FileDataset,    raw: bytes,    pixel_data_offset: int | None,    reached_pixel_data: bool,    complete: bool,    whole_file: bool,):

The result of reading a DICOM file's header in a single open.

Attributes

  • ds: The parsed dataset, stopping at the Pixel Data element. An embedded E2E record holds only its first EMBEDDED_RECORD_PREFIX_BYTES bytes.
  • raw: The bytes read from the file, less the part of each embedded E2E record past its prefix. Each such record declares the prefix's length, so raw still parses as the file's header.
  • pixel_data_offset: Offset into raw of the Pixel Data element tag, or None if the parse did not reach one.
  • reached_pixel_data: Whether the parse stopped because it found a Pixel Data element. False with complete True means the file genuinely has no pixel data (e.g. a structured report).
  • complete: Whether raw holds the whole file, or enough of it to have reached the Pixel Data element. When False the dataset may carry silently truncated element values. Also False when the file ends inside an embedded E2E record: the file is truncated, not free of pixel data.
  • whole_file: Whether raw is the entire file, unaltered, rather than a prefix of it. Stronger than complete, which a conventional file satisfies from its first megabyte: a caller that needs the pixel data itself - the Zeiss route, whose image lives in private tags - can parse raw instead of opening the file again only when this is True.

Variables​

  • complete : bool
  • ds : pydicom.dataset.FileDataset
  • pixel_data_offset : int | None
  • raw : bytes
  • reached_pixel_data : bool
  • whole_file : bool