背景:
对接Orthanc服务,需要通过c-move将远程Orthanc中的dcm文件同步至本地。
前提条件:
本地服务需要一个单独的DICOM服务可供远程DICOM服务访问。
现提供简易版DICOM Server代码如下:
import os
from pynetdicom import AE, evt, AllStoragePresentationContexts
# 设置存储影像的文件夹
output_folder = './dicom_storage'
if not os.path.exists(output_folder):
os.makedirs(output_folder)
def create_directory(path):
if not os.path.exists(path):
os.makedirs(path)
# 事件处理程序:处理接收到的影像并保存到本地文件夹
def handle_store(event):
"""Handle a C-STORE request event."""
ds = event.dataset
ds.file_meta = event.file_meta
# 从DICOM数据集中提取所需的信息
patient_id = ds.PatientID
study_uid = ds.StudyInstanceUID
series_uid = ds.SeriesInstanceUID
# 创建新的存储路径
storage_path = os.path.join(output_folder, patient_id, study_uid, series_uid)
create_directory(storage_path)
# 为接收到的影像创建文件名
file_name = os.path.join(storage_path, f"{ds.SOPInstanceUID}.dcm")
# 将影像存储到本地
ds.save_as(file_name, write_like_original=False)
print(f"Stored DICOM file: {file_name}")
return 0x0000 # Success status
if __name__ == "__main__":
# 设置应用实体 (AE) 并支持所有存储 Presentation Contexts
ae = AE()
ae.supported_contexts = AllStoragePresentationContexts
# 添加事件处理程序
handlers = [(evt.EVT_C_STORE, handle_store)]
# 启动本地DICOM服务器,等待C-MOVE的影像传输
ae.ae_title = 'LOCAL_RECEIVER'
print("Local dicom server start...")
ae.start_server(('127.0.0.1', 11112), evt_handlers=handlers)
print("Local dicom server end...")
代码中需要的pynetdicom包,请自行安装。