diff Sphinx/source/plugins/python.rst @ 348:d8359cecdc89

pillow sample
author Sebastien Jodogne <s.jodogne@gmail.com>
date Fri, 27 Mar 2020 12:20:40 +0100
parents 04fae9d4b65f
children 60080d792f25
line wrap: on
line diff
--- a/Sphinx/source/plugins/python.rst	Fri Mar 27 11:24:19 2020 +0100
+++ b/Sphinx/source/plugins/python.rst	Fri Mar 27 12:20:40 2020 +0100
@@ -15,6 +15,8 @@
 front-end.  The coverage of the C SDK is about 75% (105 functions are
 automatically wrapped in Python out of a total of 139 functions in C).
 
+This provides much more flexibility than the Lua scripts.
+
 
 Samples
 -------
@@ -163,3 +165,38 @@
           orthanc.RestApiPost('/modalities/sample/store', resourceId)
 
   orthanc.RegisterOnChangeCallback(OnChange)
+
+
+Render a thumbnail using PIL/Pillow
+...................................
+
+.. highlight:: python
+
+::
+   
+  from PIL import Image
+  import io
+  import orthanc
+
+  def DecodeInstance(output, uri, **request):
+      if request['method'] == 'GET':
+          # Retrieve the instance ID from the regular expression (*)
+          instanceId = request['groups'][0]
+
+          # Render the instance, then open it in Python using PIL/Pillow
+          png = orthanc.RestApiGet('/instances/%s/rendered' % instanceId)
+          image = Image.open(io.BytesIO(png))
+
+          # Downsize the image as a 64x64 thumbnail
+          image.thumbnail((64, 64), Image.ANTIALIAS)
+
+          # Save the thumbnail as JPEG, then send the buffer to the caller
+          jpeg = io.BytesIO()
+          image.save(jpeg, format = "JPEG", quality = 80)
+          jpeg.seek(0)
+          output.AnswerBuffer(jpeg.read(), 'text/plain')
+
+      else:
+          output.SendMethodNotAllowed('GET')
+
+  orthanc.RegisterRestCallback('/pydicom/(.*)', DecodeInstance)  # (*)