pydicom/pydicom

GitHub: pydicom/pydicom

一个纯 Python 编写的 DICOM 文件处理库,用于以简洁的方式读取、修改和写入医疗影像数据及其元数据。

Stars: 2191 | Forks: 538

[![单元测试](https://static.pigsec.cn/wp-content/uploads/repos/cas/d0/d03cc844383e1f73d2521bb85c6370444c1fa31f3f837c7d3a799b16796f6f65.svg)](https://github.com/pydicom/pydicom/actions?query=workflow%3Aunit-tests) [![类型提示](https://github.com/pydicom/pydicom/workflows/type-hints/badge.svg)](https://github.com/pydicom/pydicom/actions?query=workflow%3Atype-hints) [![文档构建](https://circleci.com/gh/pydicom/pydicom/tree/main.svg?style=shield)](https://circleci.com/gh/pydicom/pydicom/tree/main) [![测试覆盖率](https://codecov.io/gh/pydicom/pydicom/branch/main/graph/badge.svg)](https://codecov.io/gh/pydicom/pydicom) [![Python 版本](https://img.shields.io/pypi/pyversions/pydicom.svg)](https://img.shields.io/pypi/pyversions/pydicom.svg) [![PyPI 版本](https://badge.fury.io/py/pydicom.svg)](https://badge.fury.io/py/pydicom) [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.8034250.svg)](https://doi.org/10.5281/zenodo.8034250) # *pydicom* *pydicom* 是一个纯 Python 包,用于处理 [DICOM](https://www.dicomstandard.org/) 文件。 它允许你以简单且“Pythonic”的方式读取、修改和写入 DICOM 数据。作为一个纯 Python 包, *pydicom* 可以在 Python 运行的任何地方运行,没有任何其他要求,尽管如果你正在处理 *Pixel Data*,我们建议你也安装 [NumPy](https://numpy.org)。 请注意,*pydicom* 是一个通用的 DICOM 框架,主要关注 读取和写入 DICOM 数据集。为了保持 项目的可控性,它不处理特定 SOP 类的具体细节 或 DICOM 的其他方面。[pydicom 组织](https://github.com/pydicom)内部和外部的其他库都基于 *pydicom* 构建,并提供对 DICOM 其他方面以及更具体应用的支持。 例如 [pynetdicom](https://github.com/pydicom/pynetdicom),它是一个用于 DICOM 网络通信的 Python 库;以及 [deid](https://github.com/pydicom/deid), 它支持 DICOM 文件的匿名化处理。 ## 安装 使用 [pip](https://pip.pypa.io/en/stable/): ``` pip install pydicom ``` 使用 [conda](https://docs.conda.io/en/latest/): ``` conda install -c conda-forge pydicom ``` 有关更多信息,包括开发版本的安装说明,请参阅[安装指南](https://pydicom.github.io/pydicom/stable/tutorials/installation.html)。 ## 文档 *pydicom* 的[用户指南](https://pydicom.github.io/pydicom/stable/guides/user/index.html)、[教程](https://pydicom.github.io/pydicom/stable/tutorials/index.html)、[示例](https://pydicom.github.io/pydicom/stable/auto_examples/index.html)和[API 参考](https://pydicom.github.io/pydicom/stable/reference/index.html)文档可在 GitHub Pages 上针对[当前发布版本](https://pydicom.github.io/pydicom/stable)和[开发版本](https://pydicom.github.io/pydicom/dev)进行查阅。 ## *Pixel Data* 压缩和未压缩的 *Pixel Data* 始终可以作为 [bytes](https://docs.python.org/3/library/stdtypes.html#bytes-objects) 进行读取、修改和写入: ``` >>> from pydicom import dcmread >>> from pydicom.data import get_testdata_file >>> path = get_testdata_file("CT_small.dcm") >>> ds = dcmread(path) >>> type(ds.PixelData) >>> len(ds.PixelData) 32768 >>> ds.PixelData[:2] b'\xaf\x00' ``` 如果安装了 [NumPy](https://www.numpy.org),可以使用 [Dataset.pixel_array](https://pydicom.github.io/pydicom/stable/reference/generated/pydicom.dataset.Dataset.html#pydicom.dataset.Dataset.pixel_array) 属性将 *Pixel Data* 转换为 [ndarray](https://numpy.org/doc/stable/reference/generated/numpy.ndarray.html): ``` >>> arr = ds.pixel_array >>> arr.shape (128, 128) >>> arr array([[175, 180, 166, ..., 203, 207, 216], [186, 183, 157, ..., 181, 190, 239], [184, 180, 171, ..., 152, 164, 235], ..., [906, 910, 923, ..., 922, 929, 927], [914, 954, 938, ..., 942, 925, 905], [959, 955, 916, ..., 911, 904, 909]], dtype=int16) ``` ### 解压 *Pixel Data* #### JPEG、JPEG-LS 和 JPEG 2000 将 JPEG、JPEG-LS 或 JPEG 2000 压缩的 *Pixel Data* 转换为 ``ndarray`` 需要安装一个或多个额外的 Python 库。有关所需库的信息,请参阅 [pixel data handler 文档](https://pydicom.github.io/pydicom/stable/guides/user/image_data_handlers.html#guide-compressed)。 #### RLE 解压 RLE *Pixel Data* 只需要 NumPy,但速度可能会相当慢。你可以考虑[安装一个或多个额外的 Python 库](https://pydicom.github.io/pydicom/stable/guides/user/image_data_compression.html)来加快处理速度。 ### 压缩 *Pixel Data* 有关使用以下格式之一压缩 *Pixel Data* 的信息,请参阅相应的[编码指南](https://pydicom.github.io/pydicom/stable/guides/encoding/index.html)。这些指南涵盖了每种编码方法的具体要求,我们建议你在进行图像压缩时熟悉它们。 #### JPEG-LS、JPEG 2000 将来自 ``ndarray`` 或 ``bytes`` 对象的图像数据压缩为 JPEG-LS 或 JPEG 2000 需要安装以下内容: * JPEG-LS 需要 [pyjpegls](https://github.com/pydicom/pyjpegls) * JPEG 2000 需要 [pylibjpeg](https://github.com/pydicom/pylibjpeg) 和 [pylibjpeg-openjpeg](https://github.com/pydicom/pylibjpeg-openjpeg) 插件 #### RLE 使用 RLE 进行压缩不需要额外的包,但速度可能会相当慢。可以通过安装带有 [pylibjpeg-rle](https://github.com/pydicom/pylibjpeg-rle) 插件的 [pylibjpeg](https://github.com/pydicom/pylibjpeg) 或 [gdcm](https://github.com/tfmoraes/python-gdcm) 来加快速度。 ## 示例 文档中提供了更多[示例](https://pydicom.github.io/pydicom/stable/auto_examples/index.html)。 **更改患者 ID** ``` from pydicom import dcmread ds = dcmread("/path/to/file.dcm") # 编辑 (0010,0020) 'Patient ID' element ds.PatientID = "12345678" ds.save_as("/path/to/file_updated.dcm") ``` **显示 Pixel Data** 结合使用 [NumPy](https://numpy.org) 和 [matplotlib](https://matplotlib.org/) ``` import matplotlib.pyplot as plt from pydicom import dcmread, examples # pydicom 包含的 "ct" dataset 示例的路径 path: "pathlib.Path" = examples.get_path("ct") ds = dcmread(path) # `arr` 是一个 numpy.ndarray arr = ds.pixel_array plt.imshow(arr, cmap="gray") plt.show() ``` ## 贡献 我们都是在业余时间开发 *pydicom* 的志愿者。由于我们的资源有限,我们非常重视你的贡献,无论是 bug 修复、新的核心功能还是文档改进。有关更多信息,请阅读我们的[贡献指南](https://github.com/pydicom/pydicom/blob/main/CONTRIBUTING.md)。
标签:DICOM, Python, 医疗影像, 文件解析, 无后门, 逆向工具