-
Notifications
You must be signed in to change notification settings - Fork 0
/
dcm2jpg.py
50 lines (43 loc) · 1.9 KB
/
dcm2jpg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Takes a list of DICOM files, loops through them and uses dcm4che's dcm2jpg to dump JPG exports using a folder path like /run-name/patient-id/accession-number/seriesNumber-seriesDescription.jpg
# Run name is there to segment/partition the exports (ie avoid having too many files in one folder)
import sys, os, datetime, pydicom
dicomBase = '/path/to/dicom-files/'
jpgBase = '/path/to/jpeg-exports/'
cmdPath = '/path/to/dcm4che-5.23.2/bin/dcm2jpg'
if __name__ == '__main__':
if(len(sys.argv) < 2):
print("USAGE: python dcm2jpg.py dicom-file-list.txt run-name")
#python3 dcm2jpg.py dicom-file-list.txt run1
exit();
listFile = open(sys.argv[1])
runName = sys.argv[2]
i = 0;
print(datetime.datetime.now())
# loop through the .dcm images listed in the supplied text files
for dcmPath in listFile:
# Open the DICOM, get important attributes
dcmPath = dcmPath.rstrip('\n') # remove training new line
dicom = pydicom.dcmread(dcmPath)
pid = str(dicom.PatientID)
acn = str(dicom.AccessionNumber)
seriesDesc = "missing"
if 'SeriesDescription' in dicom:
seriesDesc = str(dicom.SeriesDescription)
seriesNum = "x"
if 'SeriesNumber' in dicom:
seriesNum = str(dicom.SeriesNumber)
# Check series description to eliminate laterals?!?
if 'lat' in seriesDesc.lower():
continue;
# Create a folder
destinationDir = f'{jpgBase}{runName}/{pid}/{acn}/'
#print(dcmPath + ' => ' + destinationDir)
os.system('mkdir -p ' + destinationDir)
fileName = seriesNum + '-' + seriesDesc.replace(' ','-').replace('(','').replace(')','') + '.jpg'
# Convert to JPG
cmd = f"{cmdPath} {dcmPath} {destinationDir}{fileName}"
#print(cmd)
os.system(cmd)
i += 1
#if( i > 10 ): break;
print(datetime.datetime.now())