4
from enthought.kiva import Canvas
5
from enthought.kiva.agg import GraphicsContextArray
6
from enthought.kiva.backend_image import Image
12
class ImageCanvas(Canvas):
13
def __init__(self, parent, id=-1, size=wx.DefaultSize):
14
Canvas.__init__(self, parent, id, size=size)
16
# Until we have an image, use a blank GraphicsContextArray
17
self.img = GraphicsContextArray((800, 600))
20
def load_image(self, filename):
21
# Create a new Image object for the new file and then make wx repaint the window
22
self.img = Image(filename)
26
def do_draw(self, gc):
27
# Use Image's abillity to draw itself onto a gc to paint the window
28
gc.draw_image(self.img, (0,0,self.img.width(), self.img.height()))
30
class ImageViewerWindow(wx.Frame):
31
def __init__(self, id=-1, title="Image Viewer", size=(800,600)):
33
wx.Frame.__init__(self, parent, id, title, size=size)
35
# Setup the menubar and file menu
37
filemenu.Append(ID_OPEN, "&Open", "Open an image file to view")
38
filemenu.Append(ID_QUIT, "&Quit", "Quit the program")
39
menubar = wx.MenuBar()
40
menubar.Append(filemenu, "&File")
41
self.SetMenuBar(menubar)
43
# Register the event IDs for each menu item to call the appropriate function
44
wx.EVT_MENU(self, ID_QUIT, self.onQuit)
45
wx.EVT_MENU(self, ID_OPEN, self.onOpen)
47
#Create an ImageCanvas to hold the image
48
self.canvas = ImageCanvas(self)
60
self, message = "Choose an image", defaultDir = os.getcwd(),
61
defaultFile = "", wildcard = "All Files (*.*)|*.*",
62
style=wx.OPEN | wx.CHANGE_DIR
64
#Show it modally. If the user clicks OK, have the ImageCanvas load the
66
if dlg.ShowModal() ==wx.ID_OK:
67
paths = dlg.GetPaths()
68
self.canvas.load_image(paths[0])
71
if __name__ == "__main__":
74
ImageViewerWindow(size=(800,600))