處理 module 'scipy.misc' has no attribute 'imresize'問題

1、問題

import scipy
from PIL import Image
from scipy import ndimage
# import scipy.misc
# from scipy.misc import imresize

my_image = "thumbs_up.jpg"

fname = "images/" + my_image
image = np.array(plt.imread(fname))
#Image.fromarray(arr).resize()
my_image = scipy.misc.imresize(image, size=(64, 64)).reshape((1, 64 * 64 * 3)).T
my_image_prediction = predict(my_image, parameters)

plt.imshow(image)
print("Your algorithm predicts: y = " + str(np.squeeze(my_image_prediction)))

運行上邊的代碼會報以下錯:python

AttributeErrorTraceback (most recent call last)
<ipython-input-25-824eb639aa80> in <module>
     10 image = np.array(plt.imread(fname))
     11 #Image.fromarray(arr).resize()
---> 12 my_image = scipy.misc.imresize(image, size=(64, 64)).reshape((1, 64 * 64 * 3)).T
     13 my_image_prediction = predict(my_image, parameters)
     14 

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

環境

  • 一、python版本:3.7.4
  • 二、scipy版本:1.2.1
  • 三、PIL版本:6.0.0

2、解決方案

一、安裝scikit-image

pip3 install scikit-image

二、調用resize()

from skimage.transform import resize
my_image = resize(image, output_shape=(64, 64)).reshape((1, 64 * 64 * 3)).T
注意:這裏的 resize()的參數與較老版本的 scipy.misc中的 imresize()有所不一樣,前者的 output_shape參數對應後者的 size參數。
相關文章
相關標籤/搜索