Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to fix "cannot import name 'imresize' error while this function importing from scipy.misc?

I'm using google colab for running python code and trying to downscale images.

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

It should downscale the images but show this error.

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

like image 490
star123 Avatar asked May 22 '19 16:05

star123


People also ask

How do I use Imresize in Python?

How to get the same results as Matlab by python. dtest = [1,2,3; 4,5,6; 7,8,9]; scale = 1.4; dim = imresize(dtest,1/scale); These two pieces of code return different results.

What is Scipy MISC?

The Scipy module misc contains a method derivative() that finds the nth derivative of a given function at a point. The syntax is given below. scipy.misc.derivative(func, x0, dx=1.0, n=1, args=(), order=3)


4 Answers

install scipy 1.1.0 by :

pip install scipy==1.1.0
like image 85
Aravinda_gn Avatar answered Sep 19 '22 00:09

Aravinda_gn


It is deprecated. Use cv2 resize function instead

import cv2
size = (80,80)
cv2.resize(img, size)
like image 30
Fahd Zaghdoudi Avatar answered Sep 18 '22 00:09

Fahd Zaghdoudi


You can use pillow as suggested in the comments. The changes for your code would be as mentioned below:

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

If your image is represented as a float you will get an error saying "Cannot handle this data type". In which case you need to convert the image to uint format like this:

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)))
like image 26
Prashant Avatar answered Sep 22 '22 00:09

Prashant


This work for me... In the imports i changed this:

from scipy.misc import imresize

for this:

from skimage.transform import resize

and example of the implementation i changed:

this:

img = imresize(img, (150, 150, 3)).astype('float32')/255.

for this:

img = resize(img, (150, 150, 3)).astype('float32')/255.

i hope this will help you too...

like image 24
Anthony Piñero Avatar answered Sep 20 '22 00:09

Anthony Piñero