Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to scipy.misc.imresize()

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.

like image 717
Artur Müller Romanov Avatar asked Aug 08 '19 13:08

Artur Müller Romanov


People also ask

What is MISC in SciPy?

misc) Various utilities that don't have another home. Note that the Python Imaging Library (PIL) is not a dependency of SciPy and therefore the pilutil module is not available on systems that don't have PIL installed.

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.

How do I reduce the size of an image in Python?

To resize an image, you call the resize() method on it, passing in a two-integer tuple argument representing the width and height of the resized image. The function doesn't modify the used image; it instead returns another Image with the new dimensions.


2 Answers

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)
like image 93
jdehesa Avatar answered Oct 21 '22 15:10

jdehesa


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))
like image 29
Talha Çelik Avatar answered Oct 21 '22 14:10

Talha Çelik