Alternative to scipy.misc.imresize()

26,933

Solution 1

You can lookup the documentation and the source code of the deprecated function. In short, using Pillow (Image.resize) you can do:

im = Image.fromarray(old_image)
size = tuple((np.array(im.size) * 0.99999).astype(int))
new_image = np.array(im.resize(size, PIL.Image.BICUBIC))

With skimage (skimage.transform.resize) you should get the same with:

size = (np.array(old_image.size) * 0.99999).astype(int)
new_image  = skimage.transform.resize(old_image, size, order=3)

Solution 2

Scipy Official Docs

imresize is now deprecated!
imresize is deprecated in SciPy 1.0.0, and will be removed in 1.3.0. Use Pillow instead:
numpy.array(Image.fromarray(arr).resize()).

from PIL import Image
resized_img = Image.fromarray(orj_img).resize(size=(new_h, new_w))
Share:
26,933
Artur Müller Romanov
Author by

Artur Müller Romanov

Updated on March 16, 2021

Comments

  • Artur Müller Romanov
    Artur Müller Romanov about 3 years

    I want to use an old script which still uses scipy.misc.imresize() which is not only deprevated but removed entirely from scipy. Instead the devs recommend to use either numpy.array(Image.fromarray(arr).resize()) or skimage.transform.resize().

    The exact code line that is no longer working is this:

    new_image = scipy.misc.imresize(old_image, 0.99999, interp = 'cubic')
    

    Unfortunately I am not exactly sure anymore what it does exactly. I'm afraid that if I start playing with older scipy versions, my newer scripts will stop working. I have been using it as part of a blurr filter. How do I make numpy.array(Image.fromarray(arr).resize()) or skimage.transform.resize() perform the same action as the above code line? Sorry for the lack of information I provide.

    Edit

    I have been able to determine what this line does. It converts an image array from this:

    [[[0.38332759 0.38332759 0.38332759]
      [0.38770704 0.38770704 0.38770704]
      [0.38491378 0.38491378 0.38491378]
      ...
    

    to this:

    [[[57 57 57]
      [59 59 59]
      [58 58 58]
      ...
    

    Edit2

    When I use jhansens approach the output is this:

    [[[ 97  97  97]
      [ 98  98  98]
      [ 98  98  98]
      ...
    

    I don't get what scipy.misc.imresize does.

  • Artur Müller Romanov
    Artur Müller Romanov over 4 years
    @ jdehesa Thank you for your effort. Please take a look at the edit section. I tried using both of your approaches but the output array is exactly the same as the input array. Something is missing. I hope the edit section helps.
  • jdehesa
    jdehesa over 4 years
    @ArturMüllerRomanov The function downscales the input image by a factor of 0.99999. Unless the image is very big (100,000 pixels tall or wide), that means it will just remove a single pixel in each dimension (due to float truncation). For the most part, both images should look the same, except for the slight size difference. I am pretty sure the transformation between the two arrays you show in the updated post is not produced by the function that you mention.
  • Artur Müller Romanov
    Artur Müller Romanov over 4 years
    You are right. I don't know how the values I get come to be. scipy.misc.imresize does something behind the curtains.
  • saichand
    saichand about 4 years
    To add, this resize module would give us the PIL.Image.Image object. To get the numpy array, resized_img = np.array(resized_img)