Skip to content

Instantly share code, notes, and snippets.

@jasonklotzer
Last active May 4, 2024 17:00
Show Gist options
  • Save jasonklotzer/bc6422a95f55b8c0d0924201c1bda4d8 to your computer and use it in GitHub Desktop.
Save jasonklotzer/bc6422a95f55b8c0d0924201c1bda4d8 to your computer and use it in GitHub Desktop.
Bash script to create new DICOM studies based on an existing template (uses dcmtk tools).
#!/bin/bash
# Make sure you have dcmtk installed (sudo apt install dcmtk).
DCMODIFY=dcmodify
if [ $# -ne 4 ]
then
echo "Usage : $0 inputfile outputdir #series #instances"
exit
fi
INPUTFILE=$1 # DICOM file path, used as a template
DIRECTORY=$2 # Directory path, used as the output for the generated files
SERIES=$3 # Number of series to generate
INSTANCES=$4 # Number of instances to generate
TMPFILE=$(mktemp /tmp/makestudy.XXX)
STUDYDATE=$(date +"%Y%m%d")
STUDYTIME=$(date +"%H%M%S")
STUDYPN="PATIENT^$STUDYTIME"
echo "Creating patient $STUDYPN, studydate=$STUDYDATE, studytime=$STUDYTIME"
# Check for the directory; create it if needed
if [ ! -d "$DIRECTORY" ]; then
mkdir -p $DIRECTORY
fi
echo "Using file $TMPFILE as the template"
# Create the template file
cp $INPUTFILE $TMPFILE
$DCMODIFY -gst -gse -nb -m "PatientName=$STUDYPN" -m "StudyDate=$STUDYDATE" -m "StudyTime=$STUDYTIME" $TMPFILE
# Copy the template file to the directory and modify it
for (( s=1; s<=$SERIES; s++ ))
do
$DCMODIFY -gse -nb -m "SeriesNumber=$s" $TMPFILE
for (( i=1; i<=$INSTANCES; i++ ))
do
NEWFILE=$DIRECTORY/$s\_$i
echo "Creating $NEWFILE"
cp $TMPFILE $NEWFILE
$DCMODIFY -nb -gin -m "InstanceNumber=$i" $NEWFILE
done
done
# Optional: Make a DICOMDIR
#pushd $DIRECTORY
#dcmmkdir *
#popd
# Optional: Zip the directory
#zip -r study.zip $DIRECTORY
rm $TMPFILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment