14

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'

giser_yugang
  • 6,058
  • 4
  • 21
  • 44
star123
  • 323
  • 2
  • 3
  • 9
  • 1
    `imresize` has been removed from scipy as of version 1.3, as it is a duplicate of functionality available elsewhere - specifically, the Pillow module. – jasonharper May 23 '19 at 02:12

7 Answers7

6

install scipy 1.1.0 by :

pip install scipy==1.1.0
aravinda_gn
  • 1,263
  • 1
  • 11
  • 20
3

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

Anthony Piñero
  • 616
  • 5
  • 10
3

It is deprecated. Use cv2 resize function instead

import cv2
size = (80,80)
cv2.resize(img, size)
Fahd Zaghdoudi
  • 141
  • 1
  • 13
1

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)))
Seppo Enarvi
  • 3,219
  • 3
  • 32
  • 25
Prashant
  • 434
  • 5
  • 5
1

You can try this one:

skimage.transform.resize

https://scikit-image.org/docs/stable/auto_examples/transform/plot_rescale.html

First, import resize:

from skimage.transform import resize

Then pass in the image, x/y size values (half the original in this example) and anti-aliasing set to true to prevent aliasing artifacts:

image_resized = resize(image, (image.shape[0] // 2, image.shape[1] // 2), anti_aliasing=True)

0

imresize is not available after 1.3.0.

You can use Image.resize from PIL.

install pilow:

pip install Pillow

Then import

from PIL import Image.resize as imresize
Wolf
  • 9,679
  • 7
  • 62
  • 108
  • importing Image.resize won't work at least for PIL 9.1.0. You should import Image first and then call resize for the object. Check documentation here: https://pillow.readthedocs.io/en/stable/reference/Image.html?highlight=resize#PIL.Image.Image.resize – Vyachez Apr 14 '22 at 17:17
-1

I had a similar problem and solve like this:

import scpy

and the used the functio as it is scipy.misc.imresize(args) Paulo

Dharman
  • 30,962
  • 25
  • 85
  • 135