批量裁剪图片并保存 from PIL import Image for i in range(1, 21): I = Image.open('File_OpenPath/%d.bmp'%(i)) L = I.resize((512, 512)) L.save('File_SavePath/%d.bmp' %(i)) 对复杂文件名图片进行处理 # 例如文件名为im0001.jpg到im9999.jpg. from PIL import Image for i in range(1, 9999): img = Image.open('File_OpenPath/im' + '%04d.jpg'%(i)) #保留四位有效数字,不足四位的补0。 '%04d' img.save('File_SavePath/%d.bmp'%(i)) 使用Opencv处理图片 import cv2 for i in range(1, 21): img = cv2.imread('File_OpenPath/%d.bmp'%(i)) #打开图片路径 grayImage = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #灰度化转换 reImage = cv2.resize(grayImage, (200,200)) #缩放 cv2.imwrite('File_SavePath/%d.bmp' %(i), reImage) #保存图片路径