LoginSignup
6
7

More than 3 years have passed since last update.

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

Last updated at Posted at 2020-01-19

目的

misc.imresize()

で、

AttributeError: module 'scipy.misc' has no attribute 'imresize'

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

これは、シンプルで、
ぐぐると対策がわかるハズですが、
ちょっと、そういう活動が嫌ですね。
余裕があるときは、少し、調べたりします。
今回、わかったことを記事にします。

関係する環境情報

Name: scipy
Version: 1.4.1
Summary: SciPy: Scientific Library for Python
Home-page: https://www.scipy.org

エラーと対策

原因

これは、よくあるscipyのバージョンアップの関連で、
「古いバージョンのsicpyで動作していたものが、scipy version 1.3.0以降でエラーになる」シリーズ?です。

 対策1 (対策2のほうがいいと思います。)

scipyを1.2.0にする
「1.3.0ではremoveします」と警告は出ますが、動作はします。

block_1_hobo_org.py:23: DeprecationWarning: `imresize` is deprecated!
`imresize` is deprecated in SciPy 1.0.0, and will be removed in 1.3.0.
Use Pillow instead: ``numpy.array(Image.fromarray(arr).resize())``.
  obs = (misc.imresize(obs, (110, 84))) #===

 対策2

上記の警告と同時に示される
 numpy.array(Image.fromarray(arr).resize())
を使う。

対策2を使うときの注意事項

(まず、単に、imresizeという関数なくなりました。別のとこの、
   resizeを使いなさいというシンプルでとっても簡単な話なので、
   簡単なこととして、心を落ち着けて。。。。)

 注意1:resizeでのサイズの指定は、tupleです。
 注意2:imresizeとresizeは、hightとwidthの順序が逆な気がします。

具体的には、
imresize(xxx, (110, 42))だったら、
resize((42,110))
と、逆に書けばいいと思います。
ネットの説明で間違えているサイトがあるような気がします、ご注意。

補足(imresizeとresieのhelpを記載)

widthとheightの順序に注目

imresizeのhelp

(余談ですが、コマンドは、例えば、「python -m pydoc PIL.Image.Image.resize」です。 Image.Imageとなります。。。。わかる方は、わかると思いますが。。。。) 

C:XXXX>python -m pydoc PIL.Image.Image.resize
Help on function resize in PIL.Image.Image:

PIL.Image.Image.resize = resize(self, size, resample=0, box=None)
    Returns a resized copy of this image.

    :param size: The requested size in pixels, as a 2-tuple:
       (width, height).
    :param resample: An optional resampling filter.  This can be
       one of :py:attr:`PIL.Image.NEAREST`, :py:attr:`PIL.Image.BOX`,
       :py:attr:`PIL.Image.BILINEAR`, :py:attr:`PIL.Image.HAMMING`,
       :py:attr:`PIL.Image.BICUBIC` or :py:attr:`PIL.Image.LANCZOS`.
       If omitted, or if the image has mode "1" or "P", it is
       set :py:attr:`PIL.Image.NEAREST`.
       See: :ref:`concept-filters`.
    :param box: An optional 4-tuple of floats giving the region
       of the source image which should be scaled.
       The values should be within (0, 0, width, height) rectangle.
       If omitted or None, the entire source is used.
    :returns: An :py:class:`~PIL.Image.Image` object.

resieのhelp

(余談ですが、、、コマンドは、例えば、「python -m pydoc scipy.misc.imresize」です。)

C:XXXXX>python -m pydoc scipy.misc.imresize
Help on function imresize in scipy.misc:

scipy.misc.imresize = imresize(*args, **kwds)
    `imresize` is deprecated!
    `imresize` is deprecated in SciPy 1.0.0, and will be removed in 1.3.0.
    Use Pillow instead: ``numpy.array(Image.fromarray(arr).resize())``.


        Resize an image.

        This function is only available if Python Imaging Library (PIL) is installed.

        .. warning::

            This function uses `bytescale` under the hood to rescale images to use
            the full (0, 255) range if ``mode`` is one of ``None, 'L', 'P', 'l'``.
            It will also cast data for 2-D images to ``uint32`` for ``mode=None``
            (which is the default).

        Parameters
        ----------
        arr : ndarray
            The array of image to be resized.
        size : int, float or tuple
            * int   - Percentage of current size.
            * float - Fraction of current size.
            * tuple - Size of the output image (height, width).

        interp : str, optional

まとめ

エラーが出て、
ぐぐって、、、
そうするしかないですが、、、ちょっと、嫌です。
調べて記事にしました。。。。(ぐぐられて役立つつもりで記事にしてるので、矛盾しているのですが、将来の布石。。。。)

関連(本人)

深層学習とかでのPythonエラー「ImportError: cannot import name 'imread' from 'scipy.misc' 」への対処
英語と日本語、両方使ってPythonを丁寧に学ぶ。

今後

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

6
7
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
6
7