1 | # ---------------------------------------------------------------------- |
---|
2 | # EXAMPLE: Rappture <image> elements |
---|
3 | # ====================================================================== |
---|
4 | # AUTHOR: Martin Hunt, Purdue University |
---|
5 | # Copyright (c) 2015 HUBzero Foundation, LLC |
---|
6 | # |
---|
7 | # See the file "license.terms" for information on usage and |
---|
8 | # redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
9 | # ====================================================================== |
---|
10 | |
---|
11 | import Rappture |
---|
12 | import sys |
---|
13 | from PIL import Image |
---|
14 | from io import BytesIO |
---|
15 | from Rappture import RPENC_B64, RPENC_ZB64 |
---|
16 | from Rappture.encoding import decode, encode |
---|
17 | |
---|
18 | |
---|
19 | # uncomment these for debugging |
---|
20 | # sys.stdout = open('image.out', 'w') |
---|
21 | # sys.stderr = open('image.err', 'w') |
---|
22 | |
---|
23 | |
---|
24 | # rotate image data |
---|
25 | def rotate_image(data, angle): |
---|
26 | |
---|
27 | # bug workaround in some PIL versions |
---|
28 | def fileno(): |
---|
29 | raise AttributeError |
---|
30 | |
---|
31 | # open image from data and rotate |
---|
32 | image = Image.open(BytesIO(data)) |
---|
33 | rot = image.rotate(angle, expand=True) |
---|
34 | # save image to a file in memory |
---|
35 | memfile = BytesIO() |
---|
36 | memfile.fileno = fileno # required in some broken PILs |
---|
37 | rot.save(memfile, image.format) |
---|
38 | return memfile.getvalue() |
---|
39 | |
---|
40 | |
---|
41 | # open the XML file containing the run parameters |
---|
42 | rx = Rappture.PyXml(sys.argv[1]) |
---|
43 | |
---|
44 | data = rx['input.image.current'].value |
---|
45 | # image data in B64 encoded in the xml |
---|
46 | data = decode(data, RPENC_B64) |
---|
47 | |
---|
48 | angle = rx['input.(angle).current'].value |
---|
49 | angle = float(Rappture.Units.convert(angle, to='deg', units='off')) |
---|
50 | |
---|
51 | rx['output.image(outi).about.label'] = "Rotated Image" |
---|
52 | |
---|
53 | data = rotate_image(data, angle) |
---|
54 | |
---|
55 | # Image data must be B64 or ZB64 encoded. |
---|
56 | # The next two lines are equivalent. Can use RPENC_B64 or RPENC_ZB64 |
---|
57 | rx['output.image(outi).current'] = encode(data, RPENC_ZB64) |
---|
58 | #rx.put('output.image(outi).current', data, compress=True) |
---|
59 | |
---|
60 | # add a little html note |
---|
61 | htmltext = "html://<p style=\"text-align: center;\"><a href=\"angles.html\">Learn more about angles...</a></p>" |
---|
62 | rx['output.image(outi).note.contents'] = htmltext |
---|
63 | |
---|
64 | # save the updated XML describing the run... |
---|
65 | rx.close() |
---|