一、from scipy.misc import imread,imsave,imresize 报错
imageio.imread可以代替scipy.misc.imread
用pilmode代替mode
用as_gray代替flattenpilmode类型:
- ‘L’ (8-bit pixels, grayscale)
- ‘P’ (8-bit pixels, mapped to any other mode using a color palette)
- ‘RGB’ (3x8-bit pixels, true color)
- ‘RGBA’ (4x8-bit pixels, true color with transparency mask)
- ‘CMYK’ (4x8-bit pixels, color separation)
- ‘YCbCr’ (3x8-bit pixels, color video format)
- ‘I’ (32-bit signed integer pixels)
- ‘F’ (32-bit floating point pixels)
如果as_gray=True,则用‘F‘模式读取图片;
如果已经设置了pilmode且as_gray=True,图片首先根据pilmode设置的值读取,再用’F’模式压平flatten
(1)imread和imsave解决方法:
Python已将imread和imsave两个函数写入imageio库中,我们可以先安装imageio库,就可以使用imread和imsave。
pip install imageio
imageio.imsave(output_path, image)
imageio.v2.imsave(output_path, image)
(2)imresize解决方法:
方法1:在文件中直接写入imresize函数源代码,源代码中使用到numpy库与PIL库,所以需要提前安装好numpy库与PIL库,具体代码如下
import numpy as np
from PIL import Image
def imresize(arr, size, interp='bilinear', mode=None):
im = Image.fromarray(arr, mode=mode)
ts = type(size)
if np.issubdtype(ts, np.signedinteger):
percent = size / 100.0
size = tuple((np.array(im.size)*percent).astype(int))
elif np.issubdtype(type(size), np.floating):
size = tuple((np.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])
return np.array(imnew)
方法2:
将错误的代码
image = imresize(image, [height, width], interp='nearest')
使用下方代码进行替换
image = np.array(Image.fromarray(np.uint8(image)).resize(height, width))
其中,width和height需要对应修改
方法3:
# 替代scipy im.resize
def scipy_misc_imresize(arr, size, interp='bilinear', mode=None):
im = Image.fromarray(arr, mode=mode)
ts = type(size)
if np.issubdtype(ts, np.signedinteger):
percent = size / 100.0
size = tuple((np.array(im.size) * percent).astype(int))
elif np.issubdtype(type(size), np.floating):
size = tuple((np.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 np.array(imnew)
2、distutils.errors.DistutilsPlatformError: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-bu...
解决:
这个是在python目录下scripts下执行
pip3 install -i https://pypi.douban.com/simple --upgrade setuptools
如果配了环境变量哪里都可以,如果没有,就在python目录下执行
python -m pip install -i https://pypi.douban.com/simple --upgrade pip