mirror of
https://github.com/wassname/scikit-image.git
synced 2026-06-30 21:11:39 +08:00
Second part of I/O infrastructure reorganisation.
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
"""Utilities to read and write images in various formats."""
|
||||
|
||||
import pil_plugin
|
||||
import matplotlib_plugin
|
||||
import qt_plugin
|
||||
from _plugins import load as load_plugin
|
||||
from _plugins import use as use_plugin
|
||||
from _plugins import available as plugins
|
||||
|
||||
from plugin import register as register_plugin
|
||||
# Add this plugin so that we can read images by default
|
||||
import _plugins.pil_plugin as _pil_plugin
|
||||
|
||||
from sift import *
|
||||
from collection import *
|
||||
|
||||
@@ -2,7 +2,7 @@ import plugin
|
||||
|
||||
try:
|
||||
import matplotlib.pyplot as plt
|
||||
except ImportError:
|
||||
pass
|
||||
except ImportError, e:
|
||||
print e
|
||||
else:
|
||||
plugin.register('matplotlib', show=plt.imshow, save=plt.imsave)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
__all__ = ['imread']
|
||||
|
||||
import numpy as np
|
||||
import plugin
|
||||
import numpy as np
|
||||
|
||||
try:
|
||||
from PIL import Image
|
||||
@@ -51,4 +51,4 @@ def palette_is_grayscale(pil_image):
|
||||
|
||||
|
||||
if has_pil:
|
||||
plugin.register('PIL', read=imread)
|
||||
plugin.register('pil', read=imread)
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
"""
|
||||
|
||||
__all__ = ['register', 'use']
|
||||
__all__ = ['register', 'use', 'load', 'available', 'call']
|
||||
|
||||
import warnings
|
||||
|
||||
@@ -38,7 +38,7 @@ def register(name, **kwds):
|
||||
if not callable(func):
|
||||
raise ValueError('Can only register functions as plugins.')
|
||||
|
||||
plugin_store[kind].append((name, func))
|
||||
plugin_store[kind].insert(0, (name, func))
|
||||
|
||||
|
||||
def call(kind, *args, **kwargs):
|
||||
@@ -60,7 +60,11 @@ def call(kind, *args, **kwargs):
|
||||
|
||||
plugin_funcs = plugin_store[kind]
|
||||
if len(plugin_funcs) == 0:
|
||||
raise RuntimeError('No suitable plugin registered for %s' % kind)
|
||||
raise RuntimeError('''No suitable plugin registered for %s.
|
||||
|
||||
You may load I/O plugins with the `scikits.image.io.load_plugin`
|
||||
command. A list of all available plugins can be found using
|
||||
`scikits.image.io.plugins()`.''' % kind)
|
||||
|
||||
plugin = kwargs.pop('plugin', None)
|
||||
if plugin is None:
|
||||
@@ -123,8 +127,9 @@ def available(kind=None):
|
||||
Parameters
|
||||
----------
|
||||
kind : {'show', 'save', 'read'}, optional
|
||||
Display the plugin list for the given function type. If not specified,
|
||||
return a dictionary with the plugins for all functions.
|
||||
Display the plugin list for the given function type. If not
|
||||
specified, return a dictionary with the plugins for all
|
||||
functions.
|
||||
|
||||
"""
|
||||
if kind is None:
|
||||
@@ -141,3 +146,21 @@ def available(kind=None):
|
||||
d[k] = [name for (name, func) in plugin_store[k]]
|
||||
|
||||
return d
|
||||
|
||||
def load(plugin):
|
||||
"""Load the given plugin.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
plugin : str
|
||||
Name of plugin to load.
|
||||
|
||||
See Also
|
||||
--------
|
||||
plugins : List of available plugins
|
||||
|
||||
"""
|
||||
try:
|
||||
__import__('scikits.image.io._plugins.' + plugin + "_plugin")
|
||||
except ImportError:
|
||||
raise ValueError('Plugin %s not found.' % plugin)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import plugin
|
||||
from _plugin_util import prepare_for_display
|
||||
from util import prepare_for_display
|
||||
|
||||
import numpy as np
|
||||
import sys
|
||||
@@ -37,7 +37,7 @@ else:
|
||||
def show(arr, block=True):
|
||||
global app
|
||||
|
||||
if not '-q4thread' in sys.argv and app is None:
|
||||
if not '-qt4thread' in sys.argv and app is None:
|
||||
app = QApplication([])
|
||||
|
||||
arr = prepare_for_display(arr)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import numpy as np
|
||||
import plugin
|
||||
from _plugin_util import prepare_for_display
|
||||
from util import prepare_for_display
|
||||
|
||||
try:
|
||||
import wx
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
__all__ = ['imread', 'imsave', 'imshow']
|
||||
|
||||
from scikits.image.io import plugin as _plugin
|
||||
from scikits.image.io._plugins import call as call_plugin
|
||||
|
||||
def imread(fname, as_grey=False, dtype=None, plugin=None, flatten=None,
|
||||
**plugin_args):
|
||||
@@ -44,8 +44,8 @@ def imread(fname, as_grey=False, dtype=None, plugin=None, flatten=None,
|
||||
if flatten is not None:
|
||||
as_grey = flatten
|
||||
|
||||
return _plugin.call('read', fname, as_grey=as_grey, dtype=dtype,
|
||||
plugin=plugin, **plugin_args)
|
||||
return call_plugin('read', fname, as_grey=as_grey, dtype=dtype,
|
||||
plugin=plugin, **plugin_args)
|
||||
|
||||
def imsave(fname, arr, plugin=None, **plugin_args):
|
||||
"""Save an image to file.
|
||||
@@ -67,7 +67,7 @@ def imsave(fname, arr, plugin=None, **plugin_args):
|
||||
Passed to the given plugin.
|
||||
|
||||
"""
|
||||
return _plugin.call('save', fname, arr, plugin=plugin, **plugin_args)
|
||||
return call_plugin('save', fname, arr, plugin=plugin, **plugin_args)
|
||||
|
||||
def imshow(arr, plugin=None, **plugin_args):
|
||||
"""Display an image.
|
||||
@@ -87,4 +87,4 @@ def imshow(arr, plugin=None, **plugin_args):
|
||||
Passed to the given plugin.
|
||||
|
||||
"""
|
||||
return _plugin.call('show', arr, plugin=plugin, **plugin_args)
|
||||
return call_plugin('show', arr, plugin=plugin, **plugin_args)
|
||||
|
||||
@@ -3,7 +3,7 @@ import numpy as np
|
||||
|
||||
from scikits.image import data_dir
|
||||
from scikits.image.io import imread
|
||||
from scikits.image.io.pil_plugin import palette_is_grayscale
|
||||
from scikits.image.io._plugins.pil_plugin import palette_is_grayscale
|
||||
|
||||
def test_imread_flatten():
|
||||
# a color image is flattened and returned as float32
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
from numpy.testing import *
|
||||
|
||||
from scikits.image import io
|
||||
from scikits.image.io import plugin
|
||||
from scikits.image.io._plugins import plugin
|
||||
|
||||
from copy import deepcopy
|
||||
|
||||
@@ -23,7 +23,7 @@ def show_other(arr):
|
||||
|
||||
def setup_module(self):
|
||||
self.backup_plugin_store = deepcopy(plugin.plugin_store)
|
||||
plugin.register('test', read=read, save=save, show=show)
|
||||
plugin.register('testcase', read=read, save=save, show=show)
|
||||
plugin.register('other', show=show_other)
|
||||
|
||||
def teardown_module(self):
|
||||
@@ -31,13 +31,13 @@ def teardown_module(self):
|
||||
|
||||
class TestPlugin:
|
||||
def test_read(self):
|
||||
io.imread('test.png', as_grey=True, dtype='i4', plugin='test')
|
||||
io.imread('test.png', as_grey=True, dtype='i4', plugin='testcase')
|
||||
|
||||
def test_save(self):
|
||||
io.imsave('test.png', [1, 2, 3], plugin='test')
|
||||
io.imsave('test.png', [1, 2, 3], plugin='testcase')
|
||||
|
||||
def test_show(self):
|
||||
io.imshow([1, 2, 3], plugin_arg=(1, 2), plugin='test')
|
||||
io.imshow([1, 2, 3], plugin_arg=(1, 2), plugin='testcase')
|
||||
|
||||
def test_use(self):
|
||||
plugin.use('other', 'show')
|
||||
@@ -48,5 +48,12 @@ class TestPlugin:
|
||||
d = plugin.available('show')
|
||||
assert d['show'][0] == 'other'
|
||||
|
||||
def test_load(self):
|
||||
plugin.load('test')
|
||||
fname, arr = io.imsave('outfile', [1, 2, 3])
|
||||
assert_equal(fname, 'outfile')
|
||||
assert_equal(arr, [1, 2, 3])
|
||||
assert_equal(plugin.available('save')['save'][0], 'test')
|
||||
|
||||
if __name__ == "__main__":
|
||||
run_module_suite()
|
||||
|
||||
Reference in New Issue
Block a user