3
Loads and saves RGB images from disk
4
- Left-drag pans the plot.
5
- Mousewheel up and down zooms the plot in and out.
6
- Pressing "z" brings up the Zoom Box, and you can click-drag a rectangular
7
region to zoom. If you use a sequence of zoom boxes, pressing alt-left-arrow
8
and alt-right-arrow moves you forwards and backwards through the "zoom
12
# Standard library imports
15
# Major library imports
17
# Enthought library imports
18
from traits.util.resource import find_resource
19
from traits.api import File, HasTraits, Instance
20
from traitsui.api import Handler, Item, View
22
import Action, CloseAction, Menu, MenuBar, OKCancelButtons, Separator
26
import ArrayPlotData, ImageData, Plot, PlotGraphicsContext
27
from enable.component_editor import ComponentEditor
28
from chaco.tools.api import PanTool, ZoomTool
31
#-------------------------------------------------------------------------------
33
#-------------------------------------------------------------------------------
35
class DemoView(HasTraits):
37
### Public Traits ##########################################################
39
# A Plot Data object to hold our image data
40
pd = Instance(ArrayPlotData, ())
42
# A Plot object to plot our image data
46
### Private Traits #########################################################
48
# File name to load image from
49
resource_path = os.path.join('examples','basic','capitol.jpg')
50
alt_path = 'capitol.jpg'
51
image_path = find_resource('Chaco', resource_path, alt_path=alt_path,
53
_load_file = File(image_path)
55
# File name to save image to
59
### Traits Views ###########################################################
61
# This view is for a file dialog to select the 'load' filename
62
load_file_view = View(
64
buttons=OKCancelButtons,
65
kind='livemodal', # NB must use livemodal, plot objects don't copy well
70
# This view is for a file dialog to select the 'save' filename
71
save_file_view = View(
73
buttons=OKCancelButtons,
74
kind='livemodal', # NB must use livemodal, plot objects don't copy well
79
#---------------------------------------------------------------------------
80
# Public 'DemoView' interface
81
#---------------------------------------------------------------------------
83
def __init__(self, *args, **kwargs):
84
super(DemoView, self).__init__(*args, **kwargs)
86
# Create the plot object, set some options, and add some tools
87
plot = self.plot = Plot(self.pd, default_origin="top left")
88
plot.x_axis.orientation = "top"
91
plot.tools.append(PanTool(plot))
92
zoom = ZoomTool(component=plot, tool_mode="box", always_on=False)
93
plot.overlays.append(zoom)
95
# Load the default image
98
# Plot the image plot with this image
99
self.plot.img_plot("imagedata")
101
def default_traits_view(self):
102
""" Returns the default view to use for this class.
104
# NOTE: I moved the view to this method so we can declare a handler
105
# for the view. Alternatively, we could move the DemoController class
106
# to the top and declare view=Instance(HasTraits) instead.
109
editor=ComponentEditor(),
113
Menu(Action(name="Save Plot", action="save"), # see Controller for
114
Action(name="Load Plot", action="load"), # these callbacks
123
handler=DemoController
127
#---------------------------------------------------------------------------
128
# Private 'DemoView' interface
129
#---------------------------------------------------------------------------
132
# Create a graphics context of the right size
133
win_size = self.plot.outer_bounds
134
plot_gc = PlotGraphicsContext(win_size)
136
# Have the plot component into it
137
plot_gc.render_component(self.plot)
139
# Save out to the user supplied filename
140
plot_gc.save(self._save_file)
144
# Load the image with the user supplied filename
145
image = ImageData.fromfile(self._load_file)
147
# Update the plot data. NB we must extract _date from the image
148
# for the time being, until ImageData is made more friendly
149
self.pd.set_data("imagedata", image._data)
151
# Set the title and redraw
152
self.plot.title = os.path.basename(self._load_file)
153
self.plot.request_redraw()
155
# If loading fails, simply do nothing
159
#-------------------------------------------------------------------------------
160
# Class 'DemoController'
161
#-------------------------------------------------------------------------------
163
class DemoController(Handler):
165
# The HasTraits object we are a controller for
166
view = Instance(DemoView)
168
#---------------------------------------------------------------------------
169
# Public 'DemoController' interface
170
#---------------------------------------------------------------------------
172
def init(self, info):
173
""" Initializes the controls of a user interface.
174
Overridden here to assign the 'view' trait.
176
self.view = info.object
178
def save(self, ui_info):
180
Callback for the 'Save Image' menu option.
182
ui = self.view.edit_traits(view='save_file_view')
183
if ui.result == True:
186
def load(self, ui_info):
188
Callback for the 'Load Image' menu option.
190
ui = self.view.edit_traits(view='load_file_view')
191
if ui.result == True:
195
#===============================================================================
196
# # popup object that is used by the demo.py application.
197
#===============================================================================
198
# Note: we declare a 'popup' rather than a 'demo' since the menubar doesn't seem
199
# to show up in a 'panel' mode.
202
#-------------------------------------------------------------------------------
204
#-------------------------------------------------------------------------------
208
view.configure_traits()
211
#-------------------------------------------------------------------------------
213
if __name__ == "__main__":