scipy.misc.imresize的替换⽅案错误提⽰
当scipy版本>=1.3.0时,导⼊scipy.misc.imresize会出现如下错误
AttributeError:module ‘scipy.misc’ has no attribute ‘imresize’
from scipy.misc import imresize
ImportError: cannot import name ‘imresize’
原因
scipy版本>=1.3.0时,scipy模块已经移除了scipy.misc.imresize
1.0.0<=scipy版本<1.3.0时,scipy模块会提⽰scipy.misc.imresize过时,新版本中会移除此函数替换⽅案
在scipy版本==1.2.1时,调⽤scipy.misc.imresize,出现如下提⽰
resize函数c++DeprecationWarning: imresize is 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()).
因此如需与早期版本scipy库中的imresize效果⼀致,直接使⽤PIL库中的resize即可。
通过调试源码进⼀步说明
scipy测试版本:1.2.1
在测试代码中调⽤scipy.misc.imresize(img, (200, 300))
其中img为numpy.ndarray类型
通过对上述代码下断点调试,最终会定位到***\scipy\misc\pilutil.py⽂件中的imresize函数
下⾯贴出imresize函数代码
@numpy.deprecate(message="`imresize` is deprecated in SciPy 1.0.0, "
"and will be removed in 1.3.0.\n"
"Use Pillow instead: ``numpy.array(Image.fromarray(arr).resize())``.")
def imresize(arr, size, interp='bilinear', mode=None):
im = toimage(arr, mode=mode)# im为PIL.Image.Image对象
ts =type(size)
if issubdtype(ts, numpy.signedinteger):
percent = size /100.0
size =tuple((array(im.size)*percent).astype(int))
elif issubdtype(type(size), numpy.floating):
size =tuple((array(im.size)*size).astype(int))
else:
size =(size[1], size[0])
func ={'nearest':0,'lanczos':1,'bilinear':2,'bicubic':3,'cubic':3}
imnew = im.resize(size, resample=func[interp])# 调⽤PIL库中的resize函数
return fromimage(imnew)
其中第5⾏中的im为PIL.Image.Image对象,
最终对图像进⾏resize处理代码为倒数第⼆⾏,可以看到核⼼函数为PIL库中的resize函数。