Alternative to scipy.misc.imresize()

99封情书 提交于 2020-05-10 08:46:07

问题


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.


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



回答2:


It almost looks like that line was a hacky way to transform your array from a 0..1 scale to 0..255 without any actual resizing. If that is the case you could simply do the following:

new_image = (old_image * 255).astype(np.uint8)

However, I do realize that the floats in your first sample array don't quite match the integers in the second...

Update: If you combine the rescaling to 0..255 with a resizing operation, e.g. one of the ways that jdehesa pointed out in their answer, you will reproduce your expected result (up to rounding errors). However, without knowing anything else about your code, I can't imagine that its functionality depends on resizing the image by such a small amount, which is why I'm guessing the purpose of this line of code was to transform the image to 0..255 (which is better done as above).




回答3:


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


来源:https://stackoverflow.com/questions/57414277/alternative-to-scipy-misc-imresize

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!