1. From scipy.misc import imread, imsave, imresize report an error
imageio.imread can replace scipy.misc.imread
Use pilmode instead of mode
and as_gray instead of flattenpilmode type:
- ‘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)
If as_gray=True, use the 'F' mode to read the image;
if pilmode has been set and as_gray=True, the image is first read according to the value set by pilmode, and then use the 'F' mode to flatten
(1) Imread and imsave solutions:
Python has written the two functions imread and imsave into the imageio library. We can install the imageio library first, and then we can use imread and imsave.
pip install imageio
imageio.imsave(output_path, image)
imageio.v2.imsave(output_path, image)
(2) imresize solution:
Method 1: Write the source code of the imresize function directly in the file. The source code uses the numpy library and the PIL library, so you need to install the numpy library and the PIL library in advance. The specific code is as follows
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)
Method 2:
the wrong code
image = imresize(image, [height, width], interp='nearest')
Use the code below to replace
image = np.array(Image.fromarray(np.uint8(image)).resize(height, width))
Among them, width and height need to be modified accordingly
Method 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...
solve:
This is executed under scripts in the python directory
pip3 install -i https://pypi.douban.com/simple --upgrade setuptools
If the environment variable is configured, it can be used anywhere, if not, execute it in the python directory
python -m pip install -i https://pypi.douban.com/simple --upgrade pip