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

2024-06-25 05:33:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用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

它应该缩小图像的比例,但显示出这个错误。

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


Tags: from图像importnumpyimgasscipyarray
2条回答

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

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

如果你的图像被表示为一个浮点数,你会得到一个错误,说“不能处理这个数据类型”。在这种情况下,您需要将图像转换为如下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)))

相关问题 更多 >