现在的数码相机的分辨率越来越大了,随便照一张相片,动不动就是1M,2M一张,在上传到网络上的相册前,有没有想过,有必须放这么清晰的相片到网上吗,现在的免费相册有很多,但多数是有大小限制的,比如1G大小了,对于一些照相爱好者,网上的免费相册很容易就满的哦,况且说实在的,也没必要放清晰这么高的相片到网上了,不如让我们帮这些相片瘦瘦身吧。自己动手,丰衣足食。
使用PIL(Python Imaging Library)处理图片。
#coding=gbk #fileName: resizePicture.py #批量缩小图片,需要安装PIL( Python Imaging Library ) #python resizePicture.py F;\photo F:\photo2 [jpg, [ 0.5] ] import Image import os import sys def resizeOne(picFile,outFile,rate): picF = Image.open(picFile) w,h = picF.size w,h = int(w*rate),int(h*rate) newF = picF.resize( (w,h) ) newF.save( outFile ) def main(picPath,newPath,picType,rate): fileNames = os.listdir( picPath ) for file in fileNames: dotIx = file.rfind('.') fileBegin = file[:dotIx ] fileType = file[dotIx+1:] if fileType.lower()==picType: resizeOne( picPath+'\\'+file, newPath+'\\'+fileBegin+'_mini.'+fileType, 0.5 ) print newPath+'\\'+fileBegin+'_mini.'+fileType, 'saved' if __name__ == '__main__': picPath = sys.argv[1] newPath = sys.argv[2] picType = 'jpg' rate = 0.5 if len(sys.argv)>3: picType = sys.argv[3] if len(sys.argv)>4: rate = float( sys.argv[4] ) if not os.path.isdir(picPath) or not os.path.isdir(newPath): print 'The path is incorrect' sys.exit() main(picPath,newPath,picType.lower(),rate ) |
测试PIL有没有载入用 'import Image' 即可
Traceback (most recent call last):
File "", line 2, in
picPath=sys.argv[1]
IndexError: list index out of range
不知道怎么修改??
另外代码的最后一句 是否是自己设定参数调用main函数??
非常感谢
→
def main(picPath,newPath,picType='jpg',rate=0.5):