pynetdicom#
pynetdicom is a Python package that implements the DICOM networking protocol. Working with pydicom, it allows the easy creation of DICOM Application Entities, which can then act as Service Class Users (SCUs) and Service Class Providers (SCPs) by associating with other DICOM applications.
Install#
pip install pynetdicom
conda install -c conda-forge pynetdicom
For more detailed instructions, see the installation guide.
Examples#
# Request a peer AE respond to a DICOM C-ECHO
from pynetdicom import AE
from pynetdicom.sop_class import Verification
ae = AE(ae_title="MY_AE_TITLE")
ae.add_requested_context(Verification)
# Send an association request to the peer at IP 127.0.0.1, port 11112
assoc = ae.associate("127.0.0.1", 11112)
if assoc.is_established:
# Send a C-ECHO request, returns the response status as a pydicom Dataset
status = assoc.send_c_echo()
if status:
# A success response is 0x0000
print(f"C-ECHO response: 0x{status.Status:04X}")
else:
print("Connection timed out, was aborted or received an invalid response")
# Release the association
assoc.release()
else:
print("Association request rejected, aborted or never connected")
# Request a peer AE store a DICOM dataset
from pydicom import examples, dcmread
from pynetdicom import AE
from pynetdicom.sop_class import CTImageStorage
# pydicom's example CT dataset
ds = dcmread(examples.get_path("ct"))
ae = AE(ae_title="MY_AE_TITLE")
ae.add_requested_context(CTImageStorage) # Must match the dataset being sent
assoc = ae.associate("127.0.0.1", 11112)
if assoc.is_established:
# Send a C-STORE request, returns the response status as a pydicom Dataset
status = assoc.send_c_store(ds) # May also be the path to the dataset
if status:
# A success response is 0x0000
print(f"C-STORE response: 0x{status.Status:04X}")
else:
print("Connection timed out, was aborted or received an invalid response")
# Release the association
assoc.release()
else:
print("Association request rejected, aborted or never connected")
# Listen for storage requests from peer AEs
from uuid import uuid4
from pynetdicom import AE, evt, AllStoragePresentationContexts
def handle_store(event):
"""Handle a C-STORE request event."""
# Write the received dataset directly to file
with open(str(uuid4()), 'wb') as f:
f.write(event.encoded_dataset())
# Return a 'Success' status
return 0x0000
handlers = [(evt.EVT_C_STORE, handle_store)]
ae = AE(ae_title="MY_AE_TITLE")
ae.supported_contexts = AllStoragePresentationContexts
# Start listening for incoming association requests
ae.start_server(("127.0.0.1", 11112), evt_handlers=handlers)
More service class-specific code examples can be found here.
Documentation#
User guide
The user guide contains an introduction to relevant DICOM concepts and usage of pynetdicom’s core classes and functions.
Learning resources
Our collection of code examples and tutorials should help you learn the basics of creating your own SCUs and SCPs.
Service classes
The service class documentation contains information on each of the DICOM services supported by pynetdicom.
API reference
The API reference documentation contains detailed descriptions of the classes, functions, modules and other objects included in pynetdicom.