source: trunk/examples/zoo/image/image.py @ 6021

Last change on this file since 6021 was 6021, checked in by ldelgass, 9 years ago

Merge UQ and fixes from 1.4 branch

File size: 2.0 KB
Line 
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
11import Rappture
12import sys
13from PIL import Image
14from io import BytesIO
15from Rappture import RPENC_B64, RPENC_ZB64
16from 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
25def 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
42rx = Rappture.PyXml(sys.argv[1])
43
44data = rx['input.image.current'].value
45# image data in B64 encoded in the xml
46data = decode(data, RPENC_B64)
47
48angle = rx['input.(angle).current'].value
49angle = float(Rappture.Units.convert(angle, to='deg', units='off'))
50
51rx['output.image(outi).about.label'] = "Rotated Image"
52
53data = 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
57rx['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
61htmltext = "html://<p style=\"text-align: center;\"><a href=\"angles.html\">Learn more about angles...</a></p>"
62rx['output.image(outi).note.contents'] = htmltext
63
64# save the updated XML describing the run...
65rx.close()
Note: See TracBrowser for help on using the repository browser.