mirror of
https://github.com/wassname/scikit-image.git
synced 2026-07-05 01:25:41 +08:00
Added the beginning of the gtk imshow plugin. And added the generic window manager.
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
from util import prepare_for_display, window_manager
|
||||
import plugin
|
||||
|
||||
try:
|
||||
import gtk
|
||||
except ImportError:
|
||||
pass
|
||||
else:
|
||||
|
||||
window_manager.acquire()
|
||||
|
||||
class ImageWindow(gtk.Window):
|
||||
def __init__(self, arr, mgr):
|
||||
gtk.Window.__init__(self)
|
||||
self.mgr = mgr
|
||||
self.mgr.add_window(self)
|
||||
|
||||
self.connect("destroy", self.destroy)
|
||||
|
||||
width = arr.shape[1]
|
||||
height = arr.shape[0]
|
||||
rstride = arr.strides[0]
|
||||
pb = gtk.gdk.pixbuf_new_from_data(arr.data, gtk.gdk.COLORSPACE_RGB,
|
||||
False, 8, width, height, rstride)
|
||||
self.img = gtk.Image()
|
||||
self.img.set_from_pixbuf(pb)
|
||||
|
||||
self.add(self.img)
|
||||
self.img.show()
|
||||
|
||||
def destroy(self, widget, data=None):
|
||||
self.mgr.remove_window(self)
|
||||
|
||||
def all_gone():
|
||||
print 'all windows destroyed'
|
||||
|
||||
def gtk_imshow(arr):
|
||||
arr = prepare_for_display(arr)
|
||||
|
||||
iw = ImageWindow(arr, window_manager)
|
||||
iw.show()
|
||||
|
||||
def show():
|
||||
window_manager.register_callback(gtk.main_quit)
|
||||
gtk.main()
|
||||
|
||||
plugin.register('gtk', show=gtk_imshow)
|
||||
plugin.register('gtk', appshow=show)
|
||||
|
||||
@@ -8,7 +8,8 @@ import warnings
|
||||
|
||||
plugin_store = {'read': [],
|
||||
'save': [],
|
||||
'show': []}
|
||||
'show': [],
|
||||
'appshow': []}
|
||||
|
||||
def register(name, **kwds):
|
||||
"""Register an image I/O plugin.
|
||||
|
||||
@@ -2,6 +2,57 @@ import numpy as np
|
||||
|
||||
# utilities to make life easier for plugin writers.
|
||||
|
||||
class WindowManager(object):
|
||||
''' A class to keep track of spawned windows,
|
||||
and make any needed callback once all the windows,
|
||||
are closed.'''
|
||||
def __init__(self):
|
||||
self._windows = []
|
||||
self._callback = None
|
||||
self._callback_args = ()
|
||||
self._callback_kwargs = {}
|
||||
|
||||
self._gui_lock = False
|
||||
|
||||
def _check_locked(self):
|
||||
if not self._gui_lock:
|
||||
raise RuntimeError(\
|
||||
'Must first acquire the gui lock before using this image manager ')
|
||||
|
||||
def acquire(self):
|
||||
print 'lock requested while: ', self._gui_lock
|
||||
if self._gui_lock:
|
||||
raise RuntimeError(\
|
||||
'The gui lock can only be acquired by one toolkit per session')
|
||||
else:
|
||||
self._gui_lock = True
|
||||
|
||||
def add_window(self, win):
|
||||
self._windows.append(win)
|
||||
|
||||
def remove_window(self, win):
|
||||
try:
|
||||
self._windows.remove(win)
|
||||
except ValueError:
|
||||
print 'Unable to find referenced window in tracked windows.'
|
||||
print 'Ignoring...'
|
||||
else:
|
||||
print len(self._windows)
|
||||
if len(self._windows)==0:
|
||||
self._exec_callback()
|
||||
|
||||
def register_callback(self, cb, *cbargs, **cbkwargs):
|
||||
self._callback = cb
|
||||
print self._callback
|
||||
self._callback_args = cbargs
|
||||
self._callback_kwargs = cbkwargs
|
||||
|
||||
def _exec_callback(self):
|
||||
if self._callback:
|
||||
print 'calling callback'
|
||||
self._callback(*self._callback_args, **self._callback_kwargs)
|
||||
|
||||
window_manager = WindowManager()
|
||||
|
||||
def prepare_for_display(npy_img):
|
||||
'''Convert a 2D or 3D numpy array of any dtype into a
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
__all__ = ['imread', 'imsave', 'imshow']
|
||||
__all__ = ['imread', 'imsave', 'imshow', 'show']
|
||||
|
||||
from scikits.image.io._plugins import call as call_plugin
|
||||
|
||||
@@ -88,3 +88,7 @@ def imshow(arr, plugin=None, **plugin_args):
|
||||
|
||||
"""
|
||||
return call_plugin('show', arr, plugin=plugin, **plugin_args)
|
||||
|
||||
def show():
|
||||
# need some checks here, now just for testing
|
||||
return call_plugin('appshow')
|
||||
|
||||
+1687
-2225
File diff suppressed because it is too large
Load Diff
+7840
-8017
File diff suppressed because it is too large
Load Diff
@@ -1,2 +1,2 @@
|
||||
version='unbuilt-dev'
|
||||
|
||||
# THIS FILE IS GENERATED FROM THE SCIKITS.IMAGE SETUP.PY
|
||||
version='0.2dev'
|
||||
|
||||
Reference in New Issue
Block a user