orthancDatabaseBrowser.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import orthancInterface
  2. import json
  3. import chardet
  4. import io
  5. def extractJSON(data):
  6. encoding=chardet.detect(data)["encoding"]
  7. return json.loads(data.decode(encoding))
  8. class orthancDB:
  9. def __init__(self,net):
  10. self.net=net
  11. def getPatients(self):
  12. url=self.net.getCoreURL()
  13. url+='/patients'
  14. response=self.net.get(url)
  15. return extractJSON(response.data)
  16. def getPatientData(self, orthancId):
  17. return self.getData('patients',orthancId)
  18. def getStudyData(self, studyOrthancId):
  19. return self.getData('studies',studyOrthancId)
  20. def getSeriesData(self, seriesOrthancId):
  21. return self.getData('series',seriesOrthancId)
  22. def getNumpy(self,level,orthancId):
  23. url=self.net.getCoreURL()
  24. url+='/'+level+'/'+orthancId+'/numpy'
  25. response=self.net.get(url,binary=True)
  26. return io.BytesIO(response.data)
  27. def getData(self, level, orthancId):
  28. url=self.net.getCoreURL()
  29. url+='/'+level+'/'+orthancId
  30. response=self.net.get(url)
  31. return extractJSON(response.data)
  32. def selectRows(self, qfilter):
  33. obj={}
  34. obj["Level"]="Instance"
  35. obj["Query"]=qfilter
  36. jsonData=json.dumps(obj)
  37. url=self.net.getCoreURL()
  38. url+='/tools/find'
  39. response=self.net.post(url,'json',jsonData)
  40. encoding=chardet.detect(response.data)["encoding"]
  41. return json.loads(response.data.decode(encoding))
  42. def upload(self, f) :
  43. url=self.net.getCoreURL()
  44. url+='/instances'
  45. fobj=open (f, 'rb')
  46. response=self.net.post(url,'octet-stream',fobj.read())
  47. encoding=chardet.detect(response.data)["encoding"]
  48. return json.loads(response.data.decode(encoding))