diff --git a/skimage/io/_io.py b/skimage/io/_io.py index ce718742..1a938408 100644 --- a/skimage/io/_io.py +++ b/skimage/io/_io.py @@ -1,3 +1,4 @@ +import os from io import BytesIO import numpy as np @@ -5,7 +6,6 @@ import six from skimage.io._plugins import call_plugin from skimage.color import rgb2grey -from skimage._shared import six from .util import file_or_url_context @@ -95,7 +95,14 @@ def imread(fname, as_grey=False, plugin=None, flatten=None, as_grey = flatten with file_or_url_context(fname) as fname: - img = call_plugin('imread', fname, plugin=plugin, **plugin_args) + function = 'imread' + # TODO: This should probably use imghdr to get the image format. + try: + _, extension = os.path.splitext(fname) + function = function + extension + except AttributeError: # Buffers don't work with splitext + pass + img = call_plugin(function, fname, plugin=plugin, **plugin_args) if as_grey and getattr(img, 'ndim', 0) >= 3: img = rgb2grey(img) diff --git a/skimage/io/inherited_config.py b/skimage/io/inherited_config.py index 446e6db7..9cbc7857 100644 --- a/skimage/io/inherited_config.py +++ b/skimage/io/inherited_config.py @@ -32,7 +32,10 @@ class InheritedConfig(dict): super(InheritedConfig, self).__init__(config_values, **kwargs) - def get(self, key, default=None, _prev=None): + def __getitem__(self, key): + return self.get(key, _raise=True) + + def get(self, key, default=None, _raise=False): """Return best matching config value for `key`. Get value from configuration. The search for `key` is in the following @@ -94,11 +97,13 @@ class InheritedConfig(dict): 1 """ if key in self.keys(): - return self[key] + return super(InheritedConfig, self).__getitem__(key) elif default is not None: return default elif self._separator in key: return self.get(self._parent(key)) + elif _raise: + raise KeyError('%r not in %s' % (key, self.__class__.__name__)) else: return None diff --git a/skimage/io/tests/test_inherited_config.py b/skimage/io/tests/test_inherited_config.py index 5521da0b..207d3cf1 100644 --- a/skimage/io/tests/test_inherited_config.py +++ b/skimage/io/tests/test_inherited_config.py @@ -36,6 +36,12 @@ def test_contains(): assert 'imread.jpg' in config +def test_getitem(): + config = InheritedConfig({'imread': 'a'}) + assert config['imread'] == 'a' + assert config['imread.png'] == 'a' + + if __name__ == '__main__': from numpy import testing testing.run_module_suite()