Alternative to scipy.misc.imresize()

Artur Müller Romanov picture Artur Müller Romanov · Aug 8, 2019 · Viewed 18.3k times · Source

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.

Answer

jdehesa picture jdehesa · Aug 8, 2019

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)