这篇教程Python 图像缩放批量处理程序写得很实用,希望能帮到您。 #!/usr/bin/python # -*- coding:utf8 -*- #author@skillart www. import os import shutil from PIL import Image #import Image to_scale = 0.5 processIndex = 0 def resizeImg(imgPath): global processIndex fileList = [] files = os.listdir(imgPath) for f in files: filePath = imgPath + os.sep + f if(os.path.isfile(filePath)): fileList.append(f) elif(os.path.isdir(filePath)): resizeImg(filePath) for fileName in fileList: processIndex+=1 fileFullName = imgPath+os.sep+fileName suffix = fileName[fileName.rfind('.'):] if(suffix == '.bmp' or suffix == '.jpg'): #print 'processing the '+str(processIndex)+'th file:'+fileFullName img = Image.open(fileFullName) w,h = img.size tw = int(w * to_scale) th = int(h * to_scale) reImg = img.resize((tw,th),Image.ANTIALIAS) reImg.save(fileFullName) del reImg if __name__ == '__main__': scaleStr = input('input to_scale: ') to_scale = float(scaleStr) scaledPath = './2'; #缩放后的文件目录 if os.path.isdir(scaledPath): flag = input('the output dir is exist, sure to del it(y/n)') if flag == 'y' or flag == 'yes': try: shutil.rmtree(scaledPath) finally: input('remove dir failed , please removed the dir manually.') else: exit shutil.copytree('./1',scaledPath) # ./1为原图像目录 resizeImg(scaledPath) input("resize success")
超详细注释之OpenCV dlib实现人脸采集 “Name 'raw_input' is not defined”解决方案 |