1

这是我的代码:

from skimage import io,color
filename = io.imread("input00.jpg")
img = color.rgb2gray(filename,as_grey=True)
io.imshow(img)
io.show()

在第 2 行抛出错误说:

AttributeError: 'builtin_function_or_method' object has no attribute 'iterkeys'

追溯:

Traceback (most recent call last):
  File "readImg.py", line 2, in <module>
  filename = io.imread("input00.jpg")
  File "/Library/Python/2.7/site-packages/skimage/io/_io.py", line 97, in imread
img = call_plugin('imread', fname, plugin=plugin, **plugin_args)
  File "/Library/Python/2.7/site-packages/skimage/io/manage_plugins.py", line 209, in    call_plugin
   return func(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/matplotlib-1.4.x-py2.7-macosx-10.9- intel.egg/matplotlib/pyplot.py", line 2198, in imread
  return _imread(*args, **kwargs)
 File "/Library/Python/2.7/site-packages/matplotlib-1.4.x-py2.7-macosx-10.9-intel.egg/matplotlib/image.py", line 1249, in imread
  'more images' % list(six.iterkeys(handlers.keys)))
  File "/Library/Python/2.7/site-packages/six-1.7.2-py2.7.egg/six.py", line 547, in   iterkeys
  return iter(d.iterkeys(**kw))
 AttributeError: 'builtin_function_or_method' object has no attribute 'iterkeys'
4

3 回答 3

6

有同样的问题。解决方案是安装枕头(你可以从这里http://www.lfd.uci.edu/~gohlke/pythonlibs/#pillow

于 2014-10-28T14:31:59.887 回答
0

相关的代码似乎是:

if ext not in handlers:
    im = pilread(fname)
    if im is None:
        raise ValueError('Only know how to handle extensions: %s; '
                         'with PIL installed matplotlib can handle '
                         'more images' % list(six.iterkeys(handlers.keys)))
    return im

所以我认为你面临两个问题。

(1) 出于某种原因(可能是没有枕头?)您无法处理 jpg 文件。

(2) 中存在错误matplotlib,因此错误消息没有被打印出来,因为它有错误。(!)

代替

>>> list(six.iterkeys(handlers.keys))
Traceback (most recent call last):
  File "<ipython-input-13-2e5e3ad4f63f>", line 1, in <module>
    list(six.iterkeys(handlers.keys))
  File "/usr/local/lib/python2.7/dist-packages/six.py", line 490, in iterkeys
    return iter(getattr(d, _iterkeys)(**kw))
AttributeError: 'builtin_function_or_method' object has no attribute 'iterkeys'

他们应该使用

>>> list(six.iterkeys(handlers))
['png']

并直接通过handlers字典。

于 2014-06-22T23:20:54.413 回答
0

这个问题也出现在我的项目中。我的解决方法是先卸载Pillow(sudo apt-get remove python-pil;当然如果你没有安装Pillow,请先安装,再测试),再重新安装Pillow(sudo apt-get install python-pil),问题就解决了!

于 2016-05-12T01:54:23.077 回答