python-gdcm
python-gdcm provides unofficial Python 3 bindings for the Grassroots DiCoM (GDCM) C++ library. GDCM is a cross-platform library designed for handling DICOM medical files, supporting various transfer syntaxes including native, JPEG, JPEG 2000, JPEG-LS, RLE, and deflated. The library also includes XML representations of parts 3, 6, and 7 of the DICOM Standard. As of version 3.2.2, it is actively maintained with irregular but consistent updates.
Common errors
-
ModuleNotFoundError: No module named '_gdcm.gdcmswig'
cause A local directory named `_gdcm` exists in your Python path, shadowing the actual internal module.fixRename or remove the `_gdcm` directory in your current working directory or any directory where Python searches for modules before the site-packages containing `python-gdcm`. -
ImportError: DLL load failed: The specified module could not be found.
cause This error, particularly on Windows, indicates that necessary underlying C++ dynamic link libraries (DLLs) for GDCM are not found or are incompatible with your Python environment. This can happen with manual GDCM installations or when using older, non-wheel-based distributions.fixEnsure you installed `python-gdcm` via `pip install -U python-gdcm` to get pre-compiled wheels, which usually bundle necessary DLLs. Verify your Python version is compatible (`>=3.7`). If building from source, ensure all C++ dependencies (like compilers, CMake, SWIG) are correctly configured and their output is on the system PATH. -
AttributeError: module 'gdcm' has no attribute 'DataElement'
cause This error often occurs when `pydicom` attempts to use `gdcm` as a pixel data handler, but the `gdcm` module found is not the expected library (e.g., a local directory named `gdcm` or an incomplete installation) and therefore lacks the necessary attributes.fixCheck for any local directories named `gdcm` in your Python path and rename them. Ensure `python-gdcm` is correctly installed via `pip`. If using `pydicom`, consider its specific version requirements for `gdcm`.
Warnings
- gotcha When using `import gdcm`, ensure that your current working directory does not contain a folder named `_gdcm`. If such a folder exists, it can lead to a `ModuleNotFoundError: No module named '_gdcm.gdcmswig'` or other unexpected import errors due to Python's module resolution order conflicting with the internal structure of the `python-gdcm` package.
- breaking Previous versions of GDCM for Python (before the `python-gdcm` PyPI package became the primary distribution) often had complex installation procedures, especially on Windows, and could be sensitive to the Python version. The `python-gdcm` package on PyPI aims to provide pre-built wheels for common platforms, significantly simplifying installation. However, older manual installations might lead to `ImportError: DLL load failed` due to missing C++ runtime libraries or incorrect Python bindings.
- gotcha Although `python-gdcm` wraps the C++ GDCM library, direct breaking changes within the Python API between `python-gdcm` versions 3.0.x and 3.2.x are not widely documented. Most compatibility concerns arise from interactions with other Python DICOM libraries like `pydicom`, which might have their own breaking changes or specific GDCM version requirements. For example, `pydicom` v3.0.0 introduced breaking changes and may interact differently with GDCM.
Install
-
pip install -U python-gdcm
Imports
- gdcm
import _gdcm.gdcmswig
import gdcm
Quickstart
import gdcm
# Assuming 'dicom_image_file.dcm' exists in the current directory
# Or provide a full path: reader.SetFileName('/path/to/your/dicom_image_file.dcm')
reader = gdcm.ImageReader()
reader.SetFileName("dicom_image_file.dcm")
if reader.Read():
image = reader.GetImage()
print(f"DICOM image successfully read. Dimensions: {image.GetDimension(0)}x{image.GetDimension(1)}")
# Further processing, e.g., accessing pixel data:
# pixel_array = image.GetBuffer()
else:
print(f"Error: It was not possible to read 'dicom_image_file.dcm'.")