Debian Codegraph @

traitsui/examples/demo/Extras/Image_editor_demo.py

raw
1 # (C) Copyright 2004-2023 Enthought, Inc., Austin, TX
2 # All rights reserved.
3 #
4 # This software is provided without warranty under the terms of the BSD
5 # license included in LICENSE.txt and may be redistributed only under
6 # the conditions described in the aforementioned license. The license
7 # is also available online at http://www.enthought.com/licenses/BSD.txt
8 #
9 # Thanks for using Enthought open source!
10
11 """
12 A simple demonstration of how to use the ImageEditor to add a graphic element
13 to a Traits UI View.
14
15 This example needs NumPy to show an example of a dynamically created image.
16 """
17
18 # Imports:
19 from os.path import join, dirname
20
21 import numpy as np
22
23 from pyface.api import ArrayImage, Image, ImageResource
24 from traits.api import HasTraits, Str
25 from traitsui.api import View, VGroup, Item, ImageEditor
26
27 # Constants:
28
29 # The images folder is in the same folder as this file:
30 search_path = [dirname(__file__)]
31
32
33 # Define the demo class:
34 class Employee(HasTraits):
35
36 # Define the traits:
37 name = Str()
38 dept = Str()
39 email = Str()
40 picture = Image()
41 gradient = Image()
42
43 # Define the view:
44 view = View(
45 VGroup(
46 VGroup(
47 Item(
48 'name',
49 show_label=False,
50 editor=ImageEditor(
51 image=ImageResource('info', search_path=search_path)
52 ),
53 )
54 ),
55 VGroup(
56 Item('name'),
57 Item('dept'),
58 Item('email'),
59 Item(
60 'picture',
61 editor=ImageEditor(
62 scale=True,
63 preserve_aspect_ratio=True,
64 allow_upscaling=True,
65 ),
66 springy=True,
67 ),
68 Item(
69 'gradient',
70 editor=ImageEditor(
71 scale=True,
72 preserve_aspect_ratio=True,
73 allow_upscaling=True,
74 ),
75 springy=True,
76 ),
77 ),
78 ),
79 resizable=True,
80 )
81
82
83 # generate a 2D NumPy array of RGB values
84 gradient = np.empty(shape=(256, 256, 3), dtype='uint8')
85 gradient[:, :, 0] = np.arange(256).reshape(256, 1)
86 gradient[:, :, 1] = np.arange(256).reshape(1, 256)
87 gradient[:, :, 2] = np.arange(255, -1, -1).reshape(1, 256)
88
89 # Create the demo:
90 popup = Employee(
91 name='William Murchison',
92 dept='Receiving',
93 email='[email protected]',
94 picture=ImageResource('python-logo', search_path=search_path),
95 gradient=ArrayImage(data=gradient),
96 )
97
98 # Run the demo (if invoked form the command line):
99 if __name__ == '__main__':
100 popup.configure_traits()