2

一、问题

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

运行上边的代码会报如下错:

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'

环境

  • 1、python版本:3.7.4
  • 2、scipy版本:1.2.1
  • 3、PIL版本:6.0.0

二、解决方案

1、安装scikit-image

pip3 install scikit-image

2、调用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参数。

Corwien
6.3k 声望1.6k 粉丝

为者常成,行者常至。