imread()获得了意外的关键字参数'flatten'

时间:2019-02-15 07:11:11

标签: python

以下代码显示错误。请帮助解决此问题。

    filepath = os.path.join(data_dir, 'train', img_name)
    img = imread(filepath, flatten=True)

错误图片:

TypeError

4 个答案:

答案 0 :(得分:1)

尝试

from scipy import ndimage
filepath = os.path.join(data_dir, 'train', img_name)
img = ndimage.imread(filepath, flatten=True)

答案 1 :(得分:0)

也许您使用的是matplotlib.pyplot的imread而不是scipy.ndimage.imread,反之亦然。

答案 2 :(得分:0)

似乎imread()没有关键字参数flatten

例如:

In [3]: def poi(a, b):
   ...:     print a
   ...:     

In [4]: poi(c=12)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-b07b2111af2e> in <module>()
----> 1 poi(c=12)

TypeError: poi() got an unexpected keyword argument 'c'

您需要检查函数的参数以了解其接受的内容。

答案 3 :(得分:0)

scipy中的

imread()已过时,建议使用imageio.imread()。可以找到here的过渡到imageio版本的指南。据此,您的代码应更改为:

import imageio

filepath = os.path.join(data_dir, 'train', img_name)
img = imageio.imread(filepath, as_gray=True)