LoginSignup
13
12

More than 3 years have passed since last update.

深層学習とかでのPythonエラー「ImportError: cannot import name 'imread' from 'scipy.misc' 」への対処

Last updated at Posted at 2019-07-28

目的

from scipy.misc import imread

で、

ImportError: cannot import name 'imread' from 'scipy.misc'

というエラーが出る場合がある。

これは、シンプルで、
ぐぐると対策がわかるハズですが、
ちょっと、そういう活動が嫌ですね。簡単に答えがわかれば、いいという問題じゃない気もします。。。。

エラーと対策

imread

のimportの仕方として、

>>> from scipy.misc import imread
>>> from scipy.misc.pilutil import imread
>>> from imageio import imread

がある。

選ばないとエラーになるのは、scipyの仕様が変わったためですが。。。。

scipy version 1.1.0

なんでもOKになります。

>>> import scipy
>>> scipy.__version__
'1.1.0'
>>> from scipy.misc import imread
>>> from scipy.misc.pilutil import imread
>>> from imageio import imread
>>>


scipy version 1.2.0

なんでもOKになります。version1.1.0に同じです。

>>>
>>> import scipy
>>> scipy.__version__
'1.2.0'
>>> from scipy.misc import imread
>>> from scipy.misc.pilutil import imread
>>> from imageio import imread
>>>

scipy version 1.3.0

このバージョンでは、エラーを出さないためには、
imageio import imread
が、解です。

>>> import scipy
>>> scipy.__version__
'1.3.0'
>>> from scipy.misc import imread
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'imread' from 'scipy.misc' (C:\Users\XYZZZ\AppData\Local\Programs\Python\Python37\lib\site-packages\scipy\misc\__init__.py)
>>> from scipy.misc.pilutil import imread
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'scipy.misc.pilutil'
>>> from imageio import imread
>>>
>>>
>>>

補足

imreadは、Deprecatedとは、古くから指定されている。
以下は、scipy(1.1.0)のhelpより

>>> import scipy.misc
>>> scipy.__version__
'1.1.0'

>>> help(scipy.misc)
Help on package scipy.misc in scipy:

NAME
    scipy.misc

DESCRIPTION
    ==========================================
    Miscellaneous routines (:mod:`scipy.misc`)
    ==========================================

    .. currentmodule:: scipy.misc

    Various utilities that don't have another home.

    Note that Pillow (https://python-pillow.org/) is not a dependency
    of SciPy, but the image manipulation functions indicated in the list
    below are not available without it.

    .. autosummary::
       :toctree: generated/

       ascent - Get example image for processing
       central_diff_weights - Weights for an n-point central m-th derivative
       derivative - Find the n-th derivative of a function at a point
       face - Get example image for processing
       electrocardiogram - Load an example of a one-dimensional signal.

    Deprecated functions:

    .. autosummary::
       :toctree: generated/

       bytescale - Byte scales an array (image) [requires Pillow]
       fromimage - Return a copy of a PIL image as a numpy array [requires Pillow]
       imfilter - Simple filtering of an image [requires Pillow]
       ★imread - Read an image file from a filename [requires Pillow]
       imresize - Resize an image [requires Pillow]
       imrotate - Rotate an image counter-clockwise [requires Pillow]
       imsave - Save an array to an image file [requires Pillow]
       imshow - Simple showing of an image through an external viewer [requires Pillow]
       toimage - Takes a numpy array and returns a PIL image [requires Pillow]


    Deprecated aliases:

    .. autosummary::
-- More  --

まとめ

エラーが出て、
ぐぐって、、、
そうするしかないですが、、、ちょっと、嫌です。

関連(本人)

深層学習とかでのPythonエラー「AttributeError: module 'scipy.misc' has no attribute 'imresize'」への対処

pythonをストレスなく使う!(generatorに詳しくなる。since1975らしい。)
pythonをストレスなく使う!(Pythonでは、すべてがオブジェクトとして実装されている)
pythonをストレスなく使う!(Pylintに寄り添う)
pythonをストレスなく使う!(ExpressionとStatement)
英語と日本語、両方使ってPythonを丁寧に学ぶ。

今後

コメントなどあれば、お願いします。:candy:
勉強します、、、、

13
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
12