Mercurial > hg > orthanc-book
comparison Sphinx/source/plugins/python/mosaic.py @ 1046:63edb430f259
mosaic
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 26 Mar 2024 09:50:04 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
1045:bb92aeb7dbde | 1046:63edb430f259 |
---|---|
1 import PIL.Image | |
2 import io | |
3 import json | |
4 import math | |
5 import orthanc | |
6 | |
7 def generate_mosaic(output, uri, **request): | |
8 # Sort the slices of the series, using the REST API of Orthanc | |
9 seriesId = request['groups'][0] | |
10 slices = json.loads(orthanc.RestApiGet('/series/%s/ordered-slices' % seriesId)) ['Slices'] | |
11 | |
12 # Retrieve the first slice of the mosaic | |
13 firstSliceBytes = orthanc.RestApiGet(slices[0] + '/preview') | |
14 firstSliceDecoded = PIL.Image.open(io.BytesIO(firstSliceBytes)) | |
15 | |
16 # Compute the size of the mosaic | |
17 sliceWidth, sliceHeight = firstSliceDecoded.size | |
18 side = math.ceil(math.sqrt(len(slices))) | |
19 | |
20 # Create a PIL image to store the mosaic | |
21 image = PIL.Image.new(mode = 'L', size = (side * sliceWidth, side * sliceHeight)) | |
22 | |
23 # Loop over the instances of the series to populate the mosaic | |
24 x = 0 | |
25 y = 0 | |
26 for i in range(len(slices)): | |
27 sliceBytes = orthanc.RestApiGet(slices[i] + '/preview') | |
28 sliceDecoded = PIL.Image.open(io.BytesIO(sliceBytes)) | |
29 | |
30 image.paste(sliceDecoded, (x * sliceWidth, y * sliceHeight)) | |
31 | |
32 x += 1 | |
33 if x == side: | |
34 x = 0 | |
35 y += 1 | |
36 | |
37 # Answer with the mosaic encoded as a PNG image | |
38 with io.BytesIO() as png: | |
39 image.save(png, format = 'PNG') | |
40 png.seek(0) | |
41 output.AnswerBuffer(png.read(), 'image/png') | |
42 | |
43 orthanc.RegisterRestCallback('/series/(.*)/mosaic', generate_mosaic) |