pydicom.pixels.iter_pixels

pydicom.pixels.iter_pixels(src: str | PathLike[str] | BinaryIO, *, ds_out: Dataset | None = None, specific_tags: list[BaseTag | int] | None = None, indices: Iterable[int] | None = None, raw: bool = False, decoding_plugin: str = '', **kwargs: Any) Iterator[np.ndarray][source]

Yield decoded pixel data frames from src as ndarray while minimizing memory usage.

Warning

This function requires NumPy

Processing

The following processing operations on the raw pixel data are always performed:

  • Natively encoded bit-packed pixel data for a bits allocated of 1 will be unpacked.

  • Natively encoded pixel data with a photometric interpretation of "YBR_FULL_422" will have it’s sub-sampling removed.

  • The output array will be reshaped to the specified dimensions.

  • JPEG-LS or JPEG 2000 encoded data whose signedness doesn’t match the expected pixel representation will be converted to match.

If raw = False (the default) then the following processing operation will also be performed:

Examples

Iterate through all the pixel data frames in a dataset:

for arr in iter_pixels("dataset.dcm"):
    print(arr.shape)

Iterate through the even frames for a dataset with 10 frames:

with open("dataset.dcm", "rb") as f:
    for arr in iter_pixels(f, indices=range(0, 10, 2)):
        print(arr.shape)
Parameters:
  • src (str | PathLike[str] | file-like) –

  • ds_out (pydicom.dataset.Dataset, optional) – A Dataset that will be updated with the non-retired group 0x0028 image pixel module elements and the group 0x0002 file meta information elements from the dataset in src .

  • specific_tags (list[int | pydicom.tag.BaseTag], optional) – A list of additional tags from the dataset in src to be added to the ds_out dataset.

  • indices (Iterable[int] | None, optional) – If None (default) then iterate through the entire pixel data, otherwise only iterate through the frames specified by indices.

  • raw (bool, optional) – If True then yield the decoded pixel data after only minimal processing (see the processing section above). If False (default) then additional processing may be applied to convert the pixel data to it’s most commonly used form (such as converting from YCbCr to RGB).

  • decoding_plugin (str, optional) – The name of the decoding plugin to use when decoding compressed pixel data. If no decoding_plugin is specified (default) then all available plugins will be tried and the result from the first successful one yielded. For information on the available plugins for each decoder see the API documentation.

  • **kwargs – Optional keyword parameters for controlling decoding are also available, please see the decoding options documentation for more information.

Yields:

numpy.ndarray – The decoded and reshaped pixel data, with shape:

  • (rows, columns) for single plane data

  • (rows, columns, planes) for multi-plane data

A writeable ndarray is yielded by default. For native transfer syntaxes with view_only=True a read-only ndarray will be yielded.