This README provides step-by-step instructions for configuring and using two scripts:
- receiver_server.py - A DICOM node receiver server that receives DICOM files from a PACS system
- dcm4chee-downloader.py - A script to query a DICOM database and retrieve studies
- Prerequisites
- Script 1: receiver_server.py
- Script 2: dcm4chee-downloader.py
- Workflow Overview
- Troubleshooting
Before using these scripts, ensure you have:
- Python 3.x installed
- Required Python packages installed (see
requirements.txt) - Network access to the PACS server
- Proper firewall configuration to allow DICOM communication
The receiver_server.py script acts as a DICOM node receiver server. It listens for incoming DICOM files from a PACS system and stores them in a local directory structure organized by modality and patient ID.
Open receiver_server.py in your text editor or IDE.
Locate the configuration section at the top of the file (lines 16-20) and modify the following four parameters:
# Define the folder to store received DICOM files
STORAGE_FOLDER = "./output/YOUR_DATASET_NAME/"
AE_TITLE = "YOUR AET Title"
LOCAL_IP_ADDRESS = "0.0.0.0"
LOCAL_PORT_NUM = "11112"-
STORAGE_FOLDER (Line 17)
- Description: The directory where received DICOM files will be stored
- Format: A relative or absolute path ending with a forward slash
- Example:
"./output/MY_DATASET/"or"C:/DICOM_Data/MyDataset/" - Note: The script will automatically create subdirectories organized by Modality and PatientID
-
AE_TITLE (Line 18)
- Description: Your Application Entity Title (AET) - a unique identifier for your DICOM node
- Format: A string without spaces (typically uppercase)
- Example:
"MY_RECEIVER_AET"or"RH_CANMIC" - Important: This must be unique and registered with the database maintainer
-
LOCAL_IP_ADDRESS (Line 19)
- Description: The IP address on which the server will listen
- Options:
"0.0.0.0"- Listen on all available network interfaces (recommended)"127.0.0.1"- Listen only on localhost (for testing)- Your specific IP address (e.g.,
"192.168.1.100")
- Example:
"0.0.0.0"
-
LOCAL_PORT_NUM (Line 20)
- Description: The port number on which the server will listen for DICOM connections
- Format: A string containing the port number
- Example:
"11112"or"104" - Note: Ensure this port is not already in use and is not blocked by your firewall
IMPORTANT: Before running the receiver server, you must inform the database maintainer about the following three configuration values:
- AE_TITLE - Your Application Entity Title
- LOCAL_IP_ADDRESS - Your server's IP address (if using a specific IP, not
0.0.0.0) - LOCAL_PORT_NUM - The port number your server is listening on
The database maintainer needs this information to configure the PACS system to send DICOM files to your receiver server.
Once configured, run the script:
python receiver_server.pyThe server will start and display:
- Connection status
- Directory creation messages
- File storage confirmations as DICOM files are received
Received DICOM files will be organized in the following structure:
STORAGE_FOLDER/
└── Modality/
└── PatientID/
└── SOPInstanceUID.dcm
Example:
./output/MY_DATASET/
└── CT/
└── PATIENT001/
└── 1.2.840.113619.2.55.3.1234567890.dcm
└── MR/
└── PATIENT002/
└── 1.2.840.113619.2.55.3.0987654321.dcm
The receiver server supports the following DICOM storage SOP classes:
- CT Image Storage
- MR Image Storage
- RT Structure Set Storage
- Computed Radiography Image Storage
- Digital X-Ray Image Storage (Presentation and Processing)
- Secondary Capture Image Storage
- Positron Emission Tomography Image Storage
- Enhanced CT Image Storage
- Enhanced MR Image Storage
The dcm4chee-downloader.py script queries a DICOM database (PACS) and retrieves studies based on query parameters. It uses a C-FIND query to search for studies and a C-MOVE request to retrieve them to your receiver server.
Create a .env file in the same directory as dcm4chee-downloader.py. This file will contain all the connection and configuration parameters.
Add the following environment variables to your .env file:
# Your Application Entity Title (must match what you registered with the database maintainer)
CALLING_AET=YOUR_CALLING_AET
# The PACS server's Application Entity Title
CALLED_AET=PACS_SERVER_AET
# The IP address of the PACS server
PACS_IP=192.168.1.100
# The port number of the PACS server (default is 104)
PACS_PORT=104
# The destination folder where DICOM files will be saved (if using local storage)
# OR the AE_TITLE of your receiver_server.py (if using C-MOVE)
DICOM_DESTINATION=./downloaded_dicom/
# Path to the CSV file containing query parameters
QUERY_CSV=./data/query.csv
# The AE_TITLE of your receiver server (must match AE_TITLE in receiver_server.py)
DESTINATION_SERVER_AET=YOUR_RECEIVER_AET-
CALLING_AET
- Description: Your Application Entity Title for querying the PACS
- Example:
"MY_QUERY_AET" - Note: This should be registered with the database maintainer
-
CALLED_AET
- Description: The Application Entity Title of the PACS server you're querying
- Example:
"PACS_SERVER"or"DCM4CHEE" - Note: Obtain this from the database maintainer
-
PACS_IP
- Description: The IP address or hostname of the PACS server
- Example:
"192.168.1.100"or"pacs.example.com" - Note: Ensure you have network access to this address
-
PACS_PORT
- Description: The port number the PACS server is listening on
- Default:
104(standard DICOM port) - Example:
"104"or"11113"
-
DICOM_DESTINATION
- Description: The destination for retrieved DICOM files
- Options:
- Local folder path:
"./downloaded_dicom/" - Receiver server AE_TITLE:
"YOUR_RECEIVER_AET"(recommended when using receiver_server.py)
- Local folder path:
- Note: If using C-MOVE, this should be the AE_TITLE of your receiver server
-
QUERY_CSV
- Description: Path to the CSV file containing query parameters
- Example:
"./data/query.csv" - Note: See Step 3 for CSV file format
-
DESTINATION_SERVER_AET
- Description: The AE_TITLE of your receiver server (must match the AE_TITLE in receiver_server.py)
- Example:
"MY_RECEIVER_AET" - Note: This is used for C-MOVE requests to send files to your receiver server
Create a CSV file (e.g., data/query.csv) with your query parameters. The file should have two columns: the DICOM tag name and its value.
CSV Format:
QueryRetrieveLevel,STUDY
PatientID,PATIENT001
StudyDate,20240101
AccessionNumber,ACC123456Common Query Parameters:
QueryRetrieveLevel: Usually"STUDY","SERIES", or"IMAGE"PatientID: Patient identifierStudyDate: Study date (format: YYYYMMDD)AccessionNumber: Accession numberModality: Modality type (CT, MR, PET, etc.)StudyInstanceUID: Specific study UID (if known)
Example CSV file (data/query.csv):
StudyAccessControlID,AETAccessControl
RetrieveAETitle,AET
QueryRetrieveLevel,STUDYBefore running the downloader script, make sure your receiver_server.py is running and listening for incoming DICOM files. The downloader will use C-MOVE to send files to your receiver server.
Execute the script:
python dcm4chee-downloader.pyThe script will:
- Load configuration from the
.envfile - Read query parameters from the CSV file
- Establish an association with the PACS server
- Perform a C-FIND query to search for matching studies
- Display the number of studies found
- Perform C-MOVE requests for each study to transfer them to your receiver server
- Release the association
Expected Output:
Association with PACS server established.
Found 5 studies.
Receiving study: 1.2.840.113619.2.55.3.1234567890
Study 1.2.840.113619.2.55.3.1234567890 successfully retrieved.
...
Association released.
Check your receiver_server.py storage folder (configured in STORAGE_FOLDER) to verify that the DICOM files have been received and organized correctly.
The typical workflow for using both scripts together is:
-
Configure receiver_server.py
- Set STORAGE_FOLDER, AE_TITLE, LOCAL_IP_ADDRESS, LOCAL_PORT_NUM
- Inform database maintainer about AE_TITLE, LOCAL_IP_ADDRESS, LOCAL_PORT_NUM
-
Start receiver_server.py
- Run the script and keep it running
- The server will listen for incoming DICOM files
-
Configure dcm4chee-downloader.py
- Create
.envfile with PACS connection details - Create query CSV file with search parameters
- Set DESTINATION_SERVER_AET to match receiver_server.py AE_TITLE
- Create
-
Run dcm4chee-downloader.py
- The script queries the PACS database
- Retrieves matching studies using C-MOVE
- Files are sent to your receiver server
-
Files are stored
- Receiver server receives files via C-STORE
- Files are organized by Modality/PatientID in STORAGE_FOLDER
Problem: Server fails to start
- Solution: Check if the port is already in use or blocked by firewall
- Solution: Verify LOCAL_IP_ADDRESS is correct
Problem: No files being received
- Solution: Verify database maintainer has configured PACS to send to your AE_TITLE, IP, and PORT
- Solution: Check firewall allows incoming connections on LOCAL_PORT_NUM
- Solution: Ensure receiver server is running before initiating downloads
Problem: Permission errors when saving files
- Solution: Check write permissions for STORAGE_FOLDER
- Solution: Ensure STORAGE_FOLDER path is valid
Problem: "Failed to associate with the PACS server"
- Solution: Verify PACS_IP and PACS_PORT are correct
- Solution: Check network connectivity to PACS server
- Solution: Verify CALLED_AET matches the PACS server's AE_TITLE
- Solution: Check firewall allows outbound connections
Problem: "Missing required configuration in .env file"
- Solution: Verify all required variables are set in
.envfile - Solution: Check for typos in variable names
- Solution: Ensure
.envfile is in the same directory as the script
Problem: "No studies found matching the query"
- Solution: Verify query parameters in CSV file are correct
- Solution: Check if studies matching the criteria exist in the database
- Solution: Review query syntax and DICOM tag names
Problem: "Failed to retrieve study"
- Solution: Verify DESTINATION_SERVER_AET matches receiver_server.py AE_TITLE
- Solution: Ensure receiver_server.py is running
- Solution: Check network connectivity between PACS and receiver server
Problem: CSV file not found
- Solution: Verify QUERY_CSV path in
.envis correct - Solution: Check file exists and is readable
- Security: Keep your
.envfile secure and do not commit it to version control - Network: Ensure proper network configuration for DICOM communication (typically TCP/IP)
- Performance: Large datasets may take time to transfer; be patient
- Logging: Monitor console output for status updates and error messages
- Testing: Test with a small query first before running large batch operations
For issues or questions:
- Check the troubleshooting section above
- Verify all configuration parameters are correct
- Contact the database maintainer for PACS-specific issues
- Review DICOM network connectivity and firewall settings