Mercurial > hg > orthanc
annotate OrthancServer/Resources/Samples/Python/MicroCTDicomization.py @ 5491:42e6593aa78e
fix for Stone
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 10 Jan 2024 07:21:50 +0100 |
parents | 48b8dae6dc77 |
children | f7adfb22e20e |
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 |
5485
48b8dae6dc77
upgrade to year 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5240
diff
changeset
|
7 # Copyright (C) 2017-2024 Osimis S.A., Belgium |
48b8dae6dc77
upgrade to year 2024
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
5240
diff
changeset
|
8 # 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
|
9 # |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
10 # 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
|
11 # 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
|
12 # 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
|
13 # 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
|
14 # |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
15 # 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
|
16 # 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
|
17 # 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
|
18 # 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
|
19 # |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
20 # 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
|
21 # 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
|
22 |
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 # 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
|
26 # 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
|
27 # |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
28 # 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
|
29 # 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
|
30 # 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
|
31 # |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
32 # 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
|
33 # |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
34 # $ 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
|
35 # |
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 import datetime |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
38 import io |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
39 import os |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
40 import tempfile |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
41 import zipfile |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
42 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
43 import libtiff |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
44 import numpy |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
45 import pydicom |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
46 import pydicom._storage_sopclass_uids |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
47 import pydicom.datadict |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
48 import pydicom.tag |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
49 import requests |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
50 import requests.auth |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
51 |
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 ## Parameters for the DICOM-ization ## |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
55 ######################################## |
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 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
|
58 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
59 URL = 'http://localhost:8042/' |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
60 USERNAME = 'orthanc' |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
61 PASSWORD = 'orthanc' |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
62 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
63 VOXEL_WIDTH = 1 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
64 VOXEL_HEIGHT = 1 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
65 VOXEL_DEPTH = 1 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
66 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
67 TAGS = { |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
68 'PatientID' : 'Test', |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
69 'PatientName' : 'Hello^World', |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
70 '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
|
71 '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
|
72 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
73 'AccessionNumber' : None, |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
74 'AcquisitionNumber' : None, |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
75 'KVP' : None, |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
76 'Laterality' : None, |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
77 'Manufacturer' : None, |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
78 'PatientBirthDate' : '', |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
79 'PatientPosition' : None, |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
80 'PatientSex' : 'O', |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
81 'PositionReferenceIndicator' : None, |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
82 'ReferringPhysicianName' : None, |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
83 'SeriesNumber' : 1, |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
84 'StudyID' : 'Test', |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
85 } |
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 ## Application of the DICOM-ization ## |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
91 ######################################## |
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 # Add the DICOM unique identifiers |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
94 for tag in [ 'StudyInstanceUID', |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
95 'SeriesInstanceUID', |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
96 'FrameOfReferenceUID' ]: |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
97 if not tag in TAGS: |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
98 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
|
99 |
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 def CreateDicomDataset(tif, sliceIndex): |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
102 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
|
103 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
104 meta = pydicom.Dataset() |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
105 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
|
106 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
|
107 meta.TransferSyntaxUID = pydicom.uid.ImplicitVRLittleEndian |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
108 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
109 dataset = pydicom.Dataset() |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
110 dataset.file_meta = meta |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
111 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
112 dataset.is_little_endian = True |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
113 dataset.is_implicit_VR = True |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
114 dataset.SOPClassUID = meta.MediaStorageSOPClassUID |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
115 dataset.SOPInstanceUID = meta.MediaStorageSOPInstanceUID |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
116 dataset.Modality = 'CT' |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
117 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
118 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
|
119 tag = pydicom.tag.Tag(key) |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
120 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
|
121 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
|
122 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
123 assert(image.dtype == numpy.uint16) |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
124 dataset.BitsStored = 16 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
125 dataset.BitsAllocated = 16 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
126 dataset.SamplesPerPixel = 1 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
127 dataset.HighBit = 15 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
128 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
129 dataset.Rows = image.shape[0] |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
130 dataset.Columns = image.shape[1] |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
131 dataset.InstanceNumber = (sliceIndex + 1) |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
132 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
|
133 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
|
134 dataset.SliceThickness = VOXEL_DEPTH |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
135 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
|
136 dataset.RescaleIntercept = '0' |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
137 dataset.RescaleSlope = '1' |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
138 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
|
139 dataset.PhotometricInterpretation = 'MONOCHROME2' |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
140 dataset.PixelRepresentation = 1 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
141 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
142 minValue = numpy.min(image) |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
143 maxValue = numpy.max(image) |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
144 dataset.WindowWidth = maxValue - minValue |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
145 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
|
146 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
147 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
|
148 dataset.PixelData = image.tobytes() |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
149 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
150 return dataset |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
151 |
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 # 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
|
154 with tempfile.NamedTemporaryFile() as tmp: |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
155 sliceIndex = 0 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
156 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
157 # 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
|
158 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
|
159 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
|
160 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
161 # 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
|
162 info = z.getinfo(path) |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
163 if info.is_dir(): |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
164 continue |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
165 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
166 # 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
|
167 print('DICOM-izing: %s' % path) |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
168 data = z.read(path) |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
169 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
170 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
|
171 f.write(data) |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
172 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
173 # 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
|
174 try: |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
175 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
|
176 except: |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
177 # Not a TIFF file, ignore |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
178 continue |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
179 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
180 # 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
|
181 dataset = CreateDicomDataset(tif, sliceIndex) |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
182 b = io.BytesIO() |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
183 dataset.save_as(b, False) |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
184 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
185 # 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
|
186 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
|
187 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
|
188 r.raise_for_status() |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
189 |
c9e2c6d1cd62
added two Python samples: DicomizeImage.py and MicroCTDicomization.py
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
190 sliceIndex += 1 |