import cv2 import numpy as np import matplotlib.pyplot as plt import os from PIL import Image # cracking / cell / diode / hot spot /offline module path = r'C:\Users\sun\Desktop\8' path2 = r'C:\Users\sun\Desktop\8' picture_name = os.listdir(path) for picture_names in picture_name: picture_path = os.path.join(path, picture_names) img = Image.open(picture_path).convert('RGB') # 这里可能会报错 TypeError: Expected Ptr < cv::UMat > for argument '%s' # 报错原因是传入的参数不对,原本传入到cv2中的image需要是numpy.array类型,但是从Debug的信息可以看出,传进去的是字符串类型,所以报错。解决方法如下: # 这里先把图像转换成numpy数组的格式,然再用imwrite写入,如下: img_array = np.array(img) new_picture = os.path.join(path2, picture_names) cv2.imwrite(new_picture, img_array)