Skip to content

Lookup Tag

imgtools.dicom.lookup_tag cached

lookup_tag(
    keyword: str, hex_format: bool = False
) -> typing.Optional[str]

Lookup the tag for a given DICOM keyword.

Parameters:

Name Type Description Default

keyword

str

The DICOM keyword to look up.

required

hex_format

bool

If True, return the tag in hexadecimal format (default is False).

False

Returns:

Type Description
str or None

The DICOM tag as a string, or None if the keyword is invalid.

Examples:

Lookup a DICOM tag in decimal format:

>>> lookup_tag("PatientID")
'1048608'

Lookup a DICOM tag in hexadecimal format:

>>> lookup_tag(
...     "PatientID",
...     hex_format=True,
... )
'0x100020'
Source code in src/imgtools/dicom/utils.py
@functools.lru_cache(maxsize=1024)
def lookup_tag(keyword: str, hex_format: bool = False) -> Optional[str]:
    """
    Lookup the tag for a given DICOM keyword.

    Parameters
    ----------
    keyword : str
        The DICOM keyword to look up.
    hex_format : bool, optional
        If True, return the tag in hexadecimal format (default is False).

    Returns
    -------
    str or None
        The DICOM tag as a string, or None if the keyword is invalid.

    Examples
    --------

    Lookup a DICOM tag in decimal format:

    >>> lookup_tag("PatientID")
    '1048608'

    Lookup a DICOM tag in hexadecimal format:

    >>> lookup_tag(
    ...     "PatientID",
    ...     hex_format=True,
    ... )
    '0x100020'
    """
    if (tag := tag_for_keyword(keyword)) is None:
        return None
    return f"0x{tag:X}" if hex_format else str(tag)