# HG changeset patch # User Sebastien Jodogne # Date 1585308040 -3600 # Node ID d8359cecdc89464329bc19dfdc7bf4f1466c3e5c # Parent 04fae9d4b65f0c5d1c1a12bbe4e600bc6c490595 pillow sample diff -r 04fae9d4b65f -r d8359cecdc89 Sphinx/source/plugins/python.rst --- 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) # (*)