python批量图片处理简单示例
本文实例讲述了python批量图片处理。分享给大家供大家参考,具体如下:
#!/usr/bin/python #coding:utf-8 importos fromPILimportImage #源目录 MyPath='C:/Users/Eric/Desktop/python_text/20161214/test_Image/' #输出目录 OutPath='C:/Users/Eric/Desktop/python_text/20161214/outpath/' defprocessImage(filesoure,destsoure,name,imgtype): ''' filesoure是存放待转换图片的目录 destsoure是存在输出转换后图片的目录 name是文件名 imgtype是文件类型 ''' imgtype='jpeg'ifimgtype=='.jpg'else'png' #打开图片 im=Image.open(filesoure+name) #缩放比例 rate=max(im.size[0]/640.0ifim.size[0]>60else0,im.size[1]/1136.0ifim.size[1]>1136else0) ifrate: im.thumbnail((im.size[0]/rate,im.size[1]/rate)) im.save(destsoure+name,imgtype) defrun(): #切换到源目录,遍历源目录下所有图片 os.chdir(MyPath) foriinos.listdir(os.getcwd()): #检查后缀 postfix=os.path.splitext(i)[1] ifpostfix=='.jpg'orpostfix=='.png': processImage(MyPath,OutPath,i,postfix) if__name__=='__main__': run()
更多关于Python相关内容可查看本站专题:《Python图片操作技巧总结》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程》
希望本文所述对大家Python程序设计有所帮助。