File: image_slices_viewer.py

package info (click to toggle)
matplotlib 0.98.1-1%2Blenny4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 18,624 kB
  • ctags: 22,599
  • sloc: python: 76,915; cpp: 63,459; ansic: 5,353; makefile: 111; sh: 12
file content (45 lines) | stat: -rw-r--r-- 940 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import numpy
from pylab import figure, show




class IndexTracker:
    def __init__(self, ax, X):
        self.ax = ax
        ax.set_title('use scroll wheen to navigate images')

        self.X = X
        rows,cols,self.slices = X.shape
        self.ind  = self.slices/2

        self.im = ax.imshow(self.X[:,:,self.ind])
        self.update()

    def onscroll(self, event):
        print event.button
        if event.button=='up':
            self.ind = numpy.clip(self.ind+1, 0, self.slices-1)
        else:
            self.ind = numpy.clip(self.ind-1, 0, self.slices-1)


        self.update()

    def update(self):
        self.im.set_data(self.X[:,:,self.ind])
        ax.set_ylabel('slice %s'%self.ind)        
        self.im.axes.figure.canvas.draw()


fig = figure()
ax = fig.add_subplot(111)

X = numpy.random.rand(20,20,40)

tracker = IndexTracker(ax, X)



fig.canvas.mpl_connect('scroll_event', tracker.onscroll)
show()