如何修复“在从 scipy.misc 导入此函数时无法导入名称‘imresize’的错误?

sta*_*123 8 python scipy image-resizing

我正在使用 google colab 运行 python 代码并尝试缩小图像。

from keras.layers import Lambda
import tensorflow as tf
from skimage import data, io, filters
import numpy as np
from numpy import array
from numpy.random import randint
from scipy.misc import imresize
import os
import sys

import matplotlib.pyplot as plt
plt.switch_backend('agg')


# Takes list of images and provide LR images in form of numpy array
def lr_images(images_real , downscale):

    images = []
    for img in  range(len(images_real)):
        images.append(imresize(images_real[img],[images_real[img].shape[0]//downscale,images_real[img].shape[1]//downscale], interp='bicubic', mode=None))
    images_lr = array(images)
    return images_lr
Run Code Online (Sandbox Code Playgroud)

它应该缩小图像但显示此错误。

从 scipy.misc 导入 imresize 导入错误:无法导入名称“imresize”

Ara*_*_gn 6

安装 scipy 1.1.0:

pip install scipy==1.1.0
Run Code Online (Sandbox Code Playgroud)


Pra*_*ant 1

您可以按照评论中的建议使用枕头。您的代码的更改如下所示:

import PIL

images.append(np.array(PIL.Image.fromarray(images_real[img]).resize( 
      [images_real[img].shape[0]//downscale, 
    images_real[img].shape[1]//downscale],resample=PIL.Image.BICUBIC)))
Run Code Online (Sandbox Code Playgroud)

如果您的图像表示为浮点数,您将收到一条错误消息“无法处理此数据类型”。在这种情况下,您需要将图像转换为 uint 格式,如下所示:

images.append(np.array(PIL.Image.fromarray( 
    (images_real[img]*255).astype(np.uint8)).resize( 
    [images_real[img].shape[0]//downscale, 
    images_real[img].shape[1]//downscale],resample=PIL.Image.BICUBIC)))
Run Code Online (Sandbox Code Playgroud)