Mercurial > hg > orthanc
annotate OrthancServer/Resources/Samples/Python/MicroCTDicomization.py @ 5640:f7adfb22e20e
updated copyright, as Orthanc Team now replaces Osimis
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 30 May 2024 21:19:57 +0200 |
parents | 48b8dae6dc77 |
children |
rev | line source |
---|---|
5240
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
1 #!/usr/bin/python3 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
2 # -*- coding: utf-8 -*- |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
3 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
4 # Orthanc - A Lightweight, RESTful DICOM Store |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
5 # Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
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:
5240
diff
changeset
|
9 # Copyright (C) 2021-2024 Sebastien Jodogne, ICTEAM UCLouvain, Belgium |
5240
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
10 # |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
11 # This program is free software: you can redistribute it and/or |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
12 # modify it under the terms of the GNU General Public License as |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
13 # published by the Free Software Foundation, either version 3 of the |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
14 # License, or (at your option) any later version. |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
15 # |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
16 # This program is distributed in the hope that it will be useful, but |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
17 # WITHOUT ANY WARRANTY; without even the implied warranty of |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
19 # General Public License for more details. |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
20 # |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
21 # You should have received a copy of the GNU General Public License |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
22 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
23 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
24 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
25 # |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
26 # This sample Python script illustrates how to DICOM-ize a micro-CT |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
27 # acquisition, then to upload it to Orthanc. |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
28 # |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
29 # This sample assumes that the slices of the micro-CT are encoded as |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
30 # TIFF files, that are all stored inside the same ZIP archive. Make |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
31 # sure to adapt the parameters of the DICOM-ization below. |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
32 # |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
33 # The following command-line will install the required libraries: |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
34 # |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
35 # $ sudo pip3 install libtiff numpy pydicom requests |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
36 # |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
37 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
38 import datetime |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
39 import io |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
40 import os |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
41 import tempfile |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
42 import zipfile |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
43 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
44 import libtiff |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
45 import numpy |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
46 import pydicom |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
47 import pydicom._storage_sopclass_uids |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
48 import pydicom.datadict |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
49 import pydicom.tag |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
50 import requests |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
51 import requests.auth |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
52 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
53 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
54 ######################################## |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
55 ## Parameters for the DICOM-ization ## |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
56 ######################################## |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
57 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
58 ZIP = os.path.join(os.getenv('HOME'), 'Downloads', 'SpyII_mb.zip') |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
59 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
60 URL = 'http://localhost:8042/' |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
61 USERNAME = 'orthanc' |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
62 PASSWORD = 'orthanc' |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
63 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
64 VOXEL_WIDTH = 1 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
65 VOXEL_HEIGHT = 1 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
66 VOXEL_DEPTH = 1 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
67 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
68 TAGS = { |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
69 'PatientID' : 'Test', |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
70 'PatientName' : 'Hello^World', |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
71 'StudyDate' : datetime.datetime.now().strftime('%Y%m%d'), |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
72 'StudyTime' : datetime.datetime.now().strftime('%H%M%S'), |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
73 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
74 'AccessionNumber' : None, |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
75 'AcquisitionNumber' : None, |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
76 'KVP' : None, |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
77 'Laterality' : None, |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
78 'Manufacturer' : None, |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
79 'PatientBirthDate' : '', |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
80 'PatientPosition' : None, |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
81 'PatientSex' : 'O', |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
82 'PositionReferenceIndicator' : None, |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
83 'ReferringPhysicianName' : None, |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
84 'SeriesNumber' : 1, |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
85 'StudyID' : 'Test', |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
86 } |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
87 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
88 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
89 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
90 ######################################## |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
91 ## Application of the DICOM-ization ## |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
92 ######################################## |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
93 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
94 # Add the DICOM unique identifiers |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
95 for tag in [ 'StudyInstanceUID', |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
96 'SeriesInstanceUID', |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
97 'FrameOfReferenceUID' ]: |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
98 if not tag in TAGS: |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
99 TAGS[tag] = pydicom.uid.generate_uid() |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
100 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
101 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
102 def CreateDicomDataset(tif, sliceIndex): |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
103 image = tif.read_image().astype(numpy.uint16) |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
104 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
105 meta = pydicom.Dataset() |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
106 meta.MediaStorageSOPClassUID = pydicom._storage_sopclass_uids.CTImageStorage |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
107 meta.MediaStorageSOPInstanceUID = pydicom.uid.generate_uid() |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
108 meta.TransferSyntaxUID = pydicom.uid.ImplicitVRLittleEndian |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
109 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
110 dataset = pydicom.Dataset() |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
111 dataset.file_meta = meta |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
112 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
113 dataset.is_little_endian = True |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
114 dataset.is_implicit_VR = True |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
115 dataset.SOPClassUID = meta.MediaStorageSOPClassUID |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
116 dataset.SOPInstanceUID = meta.MediaStorageSOPInstanceUID |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
117 dataset.Modality = 'CT' |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
118 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
119 for (key, value) in TAGS.items(): |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
120 tag = pydicom.tag.Tag(key) |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
121 vr = pydicom.datadict.dictionary_VR(tag) |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
122 dataset.add_new(tag, vr, value) |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
123 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
124 assert(image.dtype == numpy.uint16) |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
125 dataset.BitsStored = 16 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
126 dataset.BitsAllocated = 16 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
127 dataset.SamplesPerPixel = 1 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
128 dataset.HighBit = 15 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
129 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
130 dataset.Rows = image.shape[0] |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
131 dataset.Columns = image.shape[1] |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
132 dataset.InstanceNumber = (sliceIndex + 1) |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
133 dataset.ImagePositionPatient = r'0\0\%f' % (-float(sliceIndex) * VOXEL_DEPTH) |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
134 dataset.ImageOrientationPatient = r'1\0\0\0\-1\0' |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
135 dataset.SliceThickness = VOXEL_DEPTH |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
136 dataset.ImageType = r'ORIGINAL\PRIMARY\AXIAL' |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
137 dataset.RescaleIntercept = '0' |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
138 dataset.RescaleSlope = '1' |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
139 dataset.PixelSpacing = r'%f\%f' % (VOXEL_HEIGHT, VOXEL_WIDTH) |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
140 dataset.PhotometricInterpretation = 'MONOCHROME2' |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
141 dataset.PixelRepresentation = 1 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
142 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
143 minValue = numpy.min(image) |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
144 maxValue = numpy.max(image) |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
145 dataset.WindowWidth = maxValue - minValue |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
146 dataset.WindowCenter = (minValue + maxValue) / 2.0 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
147 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
148 pydicom.dataset.validate_file_meta(dataset.file_meta, enforce_standard=True) |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
149 dataset.PixelData = image.tobytes() |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
150 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
151 return dataset |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
152 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
153 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
154 # Create a temporary file, as libtiff is not able to read from BytesIO() |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
155 with tempfile.NamedTemporaryFile() as tmp: |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
156 sliceIndex = 0 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
157 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
158 # Loop over the files in the ZIP archive, after having sorted them |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
159 with zipfile.ZipFile(ZIP, 'r') as z: |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
160 for path in sorted(z.namelist()): |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
161 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
162 # Ignore folders in the ZIP archive |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
163 info = z.getinfo(path) |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
164 if info.is_dir(): |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
165 continue |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
166 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
167 # Extract the current file from the ZIP archive, into the temporary file |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
168 print('DICOM-izing: %s' % path) |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
169 data = z.read(path) |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
170 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
171 with open(tmp.name, 'wb') as f: |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
172 f.write(data) |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
173 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
174 # Try and decode the TIFF file |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
175 try: |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
176 tif = libtiff.TIFF.open(tmp.name) |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
177 except: |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
178 # Not a TIFF file, ignore |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
179 continue |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
180 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
181 # Create a DICOM dataset from the TIFF |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
182 dataset = CreateDicomDataset(tif, sliceIndex) |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
183 b = io.BytesIO() |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
184 dataset.save_as(b, False) |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
185 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
186 # Upload the DICOM dataset to Orthanc |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
187 r = requests.post('%s/instances' % URL, b.getvalue(), |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
188 auth = requests.auth.HTTPBasicAuth(USERNAME, PASSWORD)) |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
189 r.raise_for_status() |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
190 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
191 sliceIndex += 1 |