Home  >  Article  >  Backend Development  >  Python批量按比例缩小图片脚本分享

Python批量按比例缩小图片脚本分享

WBOY
WBOYOriginal
2016-06-10 15:11:591695browse

图片太大了,上百张图用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 '哈哈完蛋啦'

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn