Mercurial > hg > orthanc-book
annotate Sphinx/source/plugins/python/pil.py @ 703:a589668768d7
moving python samples in separate files (2)
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Fri, 11 Jun 2021 10:07:12 +0200 |
parents | |
children |
rev | line source |
---|---|
703
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
1 from PIL import Image |
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
2 import io |
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
3 import orthanc |
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
4 |
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
5 def DecodeInstance(output, uri, **request): |
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
6 if request['method'] == 'GET': |
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
7 # Retrieve the instance ID from the regular expression (*) |
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
8 instanceId = request['groups'][0] |
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
9 |
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
10 # Render the instance, then open it in Python using PIL/Pillow |
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
11 png = orthanc.RestApiGet('/instances/%s/rendered' % instanceId) |
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
12 image = Image.open(io.BytesIO(png)) |
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
13 |
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
14 # Downsize the image as a 64x64 thumbnail |
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
15 image.thumbnail((64, 64), Image.ANTIALIAS) |
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
16 |
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
17 # Save the thumbnail as JPEG, then send the buffer to the caller |
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
18 jpeg = io.BytesIO() |
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
19 image.save(jpeg, format = "JPEG", quality = 80) |
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
20 jpeg.seek(0) |
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
21 output.AnswerBuffer(jpeg.read(), 'text/plain') |
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
22 |
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
23 else: |
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
24 output.SendMethodNotAllowed('GET') |
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
25 |
a589668768d7
moving python samples in separate files (2)
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
26 orthanc.RegisterRestCallback('/pydicom/(.*)', DecodeInstance) # (*) |