-
Notifications
You must be signed in to change notification settings - Fork 0
/
wadors2jpg.py
67 lines (53 loc) · 2.62 KB
/
wadors2jpg.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# Sometimes your WADO-RS implementation may NOT support returning of JPEG renders. This script fills the gap by download the DICOM instance and exporting it to JPEG on local disk.
# Search for "TODO" to find spots you need to fill out
# Loops though a list of studies from a PostgreSQL table
import sys, os, datetime, psycopg2.extras
from requests.auth import HTTPBasicAuth
from dicomweb_client.api import DICOMwebClient
from dicomweb_client.session_utils import create_session_from_auth
jpgBase = '/path/to/jpg-exports/'
cmdPath = '/path/to/dcm4che-5.23.2/bin/dcm2jpg'
dcmPath = '/tmp/temp-image.dcm'
if __name__ == '__main__':
auth = HTTPBasicAuth('TODO_dicomweb_username', 'TODO_dicomweb_password')
session = create_session_from_auth(auth)
client = DICOMwebClient(url="http://TODO.endpoint.to/wadors", session=session)
pgconn = psycopg2.connect("host='TODO_pghost' dbname='TODO_pgdb' user='TODO_pguser' password='TODO_pgpass' client_encoding='UTF8'")
pgconn.autocommit = True
pgsql = pgconn.cursor(cursor_factory=psycopg2.extras.DictCursor)
pgsql.execute("select study_id, uid from study where jpg_exported='f' order by study_id limit 10")
rows = pgsql.fetchall()
total = len(rows)
i = 0;
print(datetime.datetime.now(), flush=True)
for row in rows:
i += 1
if ((i % 100) == 0):
print("%.2f percent complete" % (i * 100 / total), flush=True)
instances = client.retrieve_study(row['uid'])
for dicom in instances:
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;
# Write to a local DICOM file
dicom.save_as(dcmPath)
# Create a folder
destinationDir = f'{jpgBase}/{pid}/{acn}/'
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)
# Mark this exam as exported
sql = f"UPDATE study SET tmp_jpg_exported='t' WHERE study_id='{row['study_id']}';"
pgsql.execute(sql)
print(datetime.datetime.now(), flush=True)