pydicom
它能做什么
pydicom 是一个用于处理医学影像文件(称为 DICOM)的工具。它可以让你读取、修改和保存这些文件中包含的信息,例如患者数据和图像细节。
适用场景
- ✓当你需要从 DICOM 文件中提取特定信息时。
- ✓当你想修改医学影像的元数据时。
- ✓用来检查 DICOM 文件的结构和内容。
技能链接
https://github.com/K-Dense-AI/scientific-agent-skills/blob/main/skills/pydicom/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“读取 DICOM 文件“patient_exam.dcm”,告诉我检查类型和图像尺寸。”
每周 5 个新技能,1 分钟读完。
工作原理
它会读取 DICOM 文件,并以有条理的方式让你访问或修改其中的数据。你可以指定要读取或保存哪些信息。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
pydicom
Use pydicom for DICOM dataset I/O and pixel processing. Version 3.0.2 is the
current stable release reviewed here. It fixes CVE-2026-32711, a crafted
DICOMDIR path-traversal issue. pydicom 3.0.2 declares Python >=3.10; its
bundled DICOM dictionary is 2024c; the current DICOM Standard reviewed here is
2026d. No PACS, DIMSE, DICOMweb endpoint, or authenticated service is exercised
by this local-file skill.
Mandatory safety boundary
- Work only with local data that the user is authorized to access.
- DICOM metadata, file names, private elements, overlays, structured content, and pixels may contain protected health information (PHI).
- Never print
Dataset, export full metadata/JSON, or log element values by default. Use a documented allowlist and aggregate output. - pydicom is a general DICOM framework, not a diagnostic viewer. Pixel output, validation, conversion, and plugin availability are not diagnostic claims.
- De-identification is profile-, purpose-, recipient-, jurisdiction-, and threat-context-specific. It requires privacy/DICOM expert verification.
- Never claim that a tag-removal script is DICOM PS3.15, HIPAA, GDPR, or other compliance. Preserve originals and audit derived outputs.
- Treat deterministic pseudonymization keys and UID maps as re-identification secrets: use least privilege and encrypted/managed secret storage, never commit, sync, log, or share them with derivatives, and define backup, rotation, revocation, and destruction procedures. A leaked key invalidates the intended separation; rotation also changes deterministic mappings.
- Set explicit input-file, file-count, frame-count, decoded-byte, and output limits before parsing untrusted or unusually large datasets.
Installation
Create or activate an isolated environment, then install the reviewed release (the pixel-stack pins below require Python 3.12+ because of NumPy):
uv pip install "pydicom==3.0.2"
Uncompressed pixel arrays and image rendering:
uv pip install "pydicom==3.0.2" "numpy==2.5.3" "Pillow==12.3.0"
Install only the transfer-syntax plugins required by the deployment:
# JPEG/JPEG-LS, JPEG 2000/HTJ2K, and faster RLE through pylibjpeg
uv pip install "numpy==2.5.3" "pylibjpeg==2.1.0" \
"pylibjpeg-libjpeg==2.4.0" "pylibjpeg-openjpeg==2.6.0" \
"pylibjpeg-rle==2.2.0"
# JPEG-LS encoder/decoder
uv pip install "numpy==2.5.3" "pyjpegls==1.5.1"
# Alternative decoder with platform-specific wheels
uv pip install "python-gdcm==3.2.6"
Plugin licenses and wheels differ by package/platform; review them before deployment. Pillow has documented decoding limitations and pydicom cautions that plugin output must be independently checked.
Native codec wheels widen the supply-chain and memory-safety boundary. For a controlled deployment, resolve these exact pins on a trusted build host, lock and verify wheel hashes/provenance, mirror approved artifacts internally, scan them, and install with hash enforcement rather than resolving from the public index at runtime.
Choose the workflow
- Need an aggregate overview: run
scripts/extract_metadata.py. - Need bounded technical checks: run
scripts/dicom_inventory.py. - Need codec deployment preflight: run
scripts/transfer_syntax_inspector.py. - Need frame/memory planning: run
scripts/pixel_frame_planner.py. - Need one non-diagnostic rendered frame: run
scripts/dicom_to_image.py. - Need a pseudonymized derivative: read the de-identification section, create
a site-reviewed action profile, then run
scripts/anonymize_dicom.pyandscripts/deidentification_audit.py. - Need to check a sensitive UID map: run
scripts/uid_mapping_validator.py.
Read datasets safely
dcmread() returns a FileDataset, a Dataset subclass with File Format
state such as file_meta, preamble, and original encoding.
from pathlib import Path
import pydicom
path = Path("authorized/input.dcm")
ds = pydicom.dcmread(
path,
stop_before_pixels=True,
specific_tags=[
"SOPClassUID",
"Modality",
"Rows",
"Columns",
"NumberOfFrames",
],
)
technical = {
"sop_class": ds.get("SOPClassUID"),
"modality": ds.get("Modality"),
"rows": ds.get("Rows"),
"columns": ds.get("Columns"),
}
Use:
stop_before_pixels=Truefor metadata-only work.specific_tags=[...]for a minimum allowlist.
…
常见问题
什么是 DICOM 文件?
DICOM 是医学图像及相关信息的标准格式,广泛应用于 X 光机、CT 和 MRI 等设备中。
我可以用 pydicom 查看图像吗?
pydicom 主要用于读取和处理文件中的数据,而不是像医学软件那样显示图像。它可以帮助你为查看图像做好数据准备。
在处理患者数据时使用 pydicom 安全吗?
可以,但需要谨慎。pydicom 会对敏感信息(PHI)发出提醒,并要求你采取保护措施,例如默认不完整显示数据,必要时进行匿名化处理。
详情
作者的原始描述
Reads, inspects, writes, transforms, and preflights local DICOM datasets and pixel data. Applies to DICOM metadata, transfer syntaxes, compression plugins, frames, private elements, JSON, and bounded de-identification review.
热度
- GitHub 星标
- 51k
- 排名
- #3539
- 许可证
- MIT
- 更新时间
- 2026/10/15
评论
- …