Mercurial > hg > orthanc
annotate OrthancServer/Resources/Samples/Python/DownloadAnonymized.py @ 5754:f75596b224e0 find-refactoring-clean tip
merged find-refactoring -> find-refactoring-clean (integration tests ok)
author | Alain Mazy <am@orthanc.team> |
---|---|
date | Wed, 04 Sep 2024 10:54:00 +0200 |
parents | f7adfb22e20e |
children |
rev | line source |
---|---|
353
61e4c62b8c35
samples of anonymization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
1 #!/usr/bin/python |
61e4c62b8c35
samples of anonymization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
61e4c62b8c35
samples of anonymization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
3 |
747
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
353
diff
changeset
|
4 # Orthanc - A Lightweight, RESTful DICOM Store |
1900 | 5 # Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
1288
6e7e5ed91c2d
upgrade to year 2015
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1184
diff
changeset
|
6 # Department, University Hospital of Liege, Belgium |
5640
f7adfb22e20e
updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5485
diff
changeset
|
7 # Copyright (C) 2017-2023 Osimis S.A., Belgium |
f7adfb22e20e
updated copyright, as Orthanc Team now replaces Osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5485
diff
changeset
|
8 # Copyright (C) 2024-2024 Orthanc Team SRL, Belgium |
5485
48b8dae6dc77
upgrade to year 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5185
diff
changeset
|
9 # Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
747
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
353
diff
changeset
|
10 # |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
353
diff
changeset
|
11 # This program is free software: you can redistribute it and/or |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
353
diff
changeset
|
12 # modify it under the terms of the GNU General Public License as |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
353
diff
changeset
|
13 # published by the Free Software Foundation, either version 3 of the |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
353
diff
changeset
|
14 # License, or (at your option) any later version. |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
353
diff
changeset
|
15 # |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
353
diff
changeset
|
16 # This program is distributed in the hope that it will be useful, but |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
353
diff
changeset
|
17 # WITHOUT ANY WARRANTY; without even the implied warranty of |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
353
diff
changeset
|
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
353
diff
changeset
|
19 # General Public License for more details. |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
353
diff
changeset
|
20 # |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
353
diff
changeset
|
21 # You should have received a copy of the GNU General Public License |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
353
diff
changeset
|
22 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
353
diff
changeset
|
23 |
44382c8bcd15
added explicit licensing terms for samples
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
353
diff
changeset
|
24 |
353
61e4c62b8c35
samples of anonymization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
25 |
2032
65b1ce7cb84f
Replaced "localhost" by "127.0.0.1", as it might impact performance on Windows
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
26 URL = 'http://127.0.0.1:8042' |
353
61e4c62b8c35
samples of anonymization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
27 |
61e4c62b8c35
samples of anonymization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
28 # |
61e4c62b8c35
samples of anonymization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
29 # This sample code will download a ZIP file for each patient that has |
61e4c62b8c35
samples of anonymization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
30 # been anonymized in Orthanc. |
61e4c62b8c35
samples of anonymization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
31 # |
61e4c62b8c35
samples of anonymization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
32 |
61e4c62b8c35
samples of anonymization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
33 import os |
61e4c62b8c35
samples of anonymization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
34 import os.path |
61e4c62b8c35
samples of anonymization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
35 import sys |
61e4c62b8c35
samples of anonymization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
36 import RestToolbox |
61e4c62b8c35
samples of anonymization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
37 |
61e4c62b8c35
samples of anonymization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
38 # Loop over the patients |
61e4c62b8c35
samples of anonymization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
39 for patient in RestToolbox.DoGet('%s/patients' % URL): |
61e4c62b8c35
samples of anonymization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
40 |
61e4c62b8c35
samples of anonymization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
41 # Ignore patients whose name starts with "Anonymized", as it is |
61e4c62b8c35
samples of anonymization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
42 # the result of a previous anonymization |
61e4c62b8c35
samples of anonymization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
43 infos = RestToolbox.DoGet('%s/patients/%s' % (URL, patient)) |
61e4c62b8c35
samples of anonymization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
44 name = infos['MainDicomTags']['PatientName'].lower() |
61e4c62b8c35
samples of anonymization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
45 if name.startswith('anonymized'): |
61e4c62b8c35
samples of anonymization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
46 |
61e4c62b8c35
samples of anonymization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
47 # Trigger the download |
1184 | 48 print('Downloading %s' % name) |
353
61e4c62b8c35
samples of anonymization
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
49 zipContent = RestToolbox.DoGet('%s/patients/%s/archive' % (URL, patient)) |
2070
7e6afa0beaf6
samples: improved handling of special characters in paths
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2032
diff
changeset
|
50 with open(os.path.join('/tmp', name + '.zip'), 'wb') as f: |
7e6afa0beaf6
samples: improved handling of special characters in paths
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2032
diff
changeset
|
51 f.write(zipContent) |