黑帽联盟
标题: Python批量缩小图片(按比例)脚本分享 [打印本页]
作者: 定位    时间: 2017-1-26 16:58
标题: Python批量缩小图片(按比例)脚本分享
这篇文章主要介绍了Python批量按比例缩小图片脚本分享,本文直接给出实现代码,需要的朋友可以参考下
图片太大了,上百张图用photoshop改太慢,就想到用python写个简单的批处理。功能简单就是把原图按比例缩小
- # -*- coding: cp936 -*- 
 
- import Image  
 
- import glob, os 
 
- #图片批处理  
 
- def timage():  
 
-     for files in glob.glob('D:\\\\1\\\\*.JPG'):  
 
-         filepath,filename = os.path.split(files)  
 
-         filterame,exts = os.path.splitext(filename)  
 
-         #输出路径  
 
-         opfile = r'D:\\\\22\\\\' 
 
-         #判断opfile是否存在,不存在则创建  
 
-         if (os.path.isdir(opfile)==False):  
 
-             os.mkdir(opfile)  
 
-         im = Image.open(files)  
 
-         w,h = im.size  
 
-         #im_ss = im.resize((400,400))  
 
-         #im_ss = im.convert('P')  
 
-         im_ss = im.resize((int(w*0.12), int(h*0.12)))  
 
-         im_ss.save(opfile+filterame+'.jpg') 
 
- if __name__=='__main__':  
 
-     timage() 
 
-     print '哈哈OK啦'
| 欢迎光临 黑帽联盟 (https://bbs.cnblackhat.com/) | Powered by Discuz! X2.5 |