Mercurial > hg > orthanc-book
annotate Sphinx/source/plugins/python/pil-conversions.py @ 763:4e9dc26a5cb9 Orthanc-1.9.7
Orthanc 1.9.7
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 31 Aug 2021 11:24:43 +0200 |
parents | 97e6a0431c5e |
children | 01fa632daae9 |
rev | line source |
---|---|
706
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
1 import json |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
2 import PIL.Image |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
3 import PIL.ImageDraw |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
4 import orthanc |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
5 |
763 | 6 URL = 'http://hg.orthanc-server.com/orthanc-tests/raw-file/Orthanc-1.9.7/Database/LenaTwiceWithFragments.dcm' |
706
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
7 USERNAME = '' |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
8 PASSWORD = '' |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
9 |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
10 def OnChange(changeType, level, resource): |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
11 if changeType == orthanc.ChangeType.ORTHANC_STARTED: |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
12 |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
13 # (1) Download a sample DICOM instance and decode it |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
14 orthanc.LogWarning('Downloading: %s' % URL) |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
15 lena = orthanc.HttpGet(URL, USERNAME, PASSWORD) |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
16 |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
17 dicom = orthanc.CreateDicomInstance(lena) |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
18 orthanc.LogWarning('Number of frames: %d' % dicom.GetInstanceFramesCount()) |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
19 |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
20 # (2) Access the first frame of the instance as a PIL image |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
21 frame = dicom.GetInstanceDecodedFrame(0) |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
22 size = (frame.GetImageWidth(), frame.GetImageHeight()) |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
23 |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
24 if frame.GetImagePixelFormat() == orthanc.PixelFormat.RGB24: |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
25 mode = 'RGB' |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
26 else: |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
27 raise Exception('Unsupported pixel format') |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
28 |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
29 image = PIL.Image.frombuffer(mode, size, frame.GetImageBuffer(), 'raw', mode, 0, 1) |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
30 |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
31 # (3) Draw a red cross over the PIL image |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
32 draw = PIL.ImageDraw.Draw(image) |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
33 draw.line((0, 0) + image.size, fill=(255,0,0), width=10) |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
34 draw.line((0, image.size[1], image.size[0], 0), fill=(255,0,0), width=10) |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
35 |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
36 # (4) Convert back the modified PIL image to an Orthanc image |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
37 buf = image.tobytes() |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
38 a = orthanc.CreateImageFromBuffer(frame.GetImagePixelFormat(), image.size[0], image.size[1], |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
39 len(buf) / image.size[1], buf) |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
40 |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
41 # (5) Create and upload a new DICOM instance with the modified frame |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
42 tags = { |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
43 'SOPClassUID' : '1.2.840.10008.5.1.4.1.1.1', |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
44 'PatientID' : 'HELLO', |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
45 'PatientName' : 'WORLD', |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
46 } |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
47 |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
48 s = orthanc.CreateDicom(json.dumps(tags), a, orthanc.CreateDicomFlags.GENERATE_IDENTIFIERS) |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
49 orthanc.RestApiPost('/instances', s) |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
50 orthanc.LogWarning('Image successfully modified and uploaded!') |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
51 |
c62539d00251
Conversions between Orthanc and Python images
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
52 orthanc.RegisterOnChangeCallback(OnChange) |