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

yipeiwu_com6年前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 '哈哈完蛋啦'

相关文章

Python通过websocket与js客户端通信示例分析

Python通过websocket与js客户端通信示例分析

具体的 websocket 介绍可见 http://zh.wikipedia.org/wiki/WebSocket  这里,介绍如何使用 Python 与前端 js 进行通信。...

python turtle 绘制太极图的实例

python turtle 绘制太极图的实例

效果如下所示: # -*- coding: utf-8 -*- import turtle # 绘制太极图函数 def draw_TJT(R):    ...

python安装oracle扩展及数据库连接方法

本文实例讲述了python安装oracle扩展及数据库连接方法。分享给大家供大家参考,具体如下: 下载: cx_Oracle下载地址:http://cx-oracle.sourcefor...

Python ValueError: invalid literal for int() with base 10 实用解决方法

今天在写爬虫程序的时候由于要翻页,做除法分页的时候出现了 复制代码 代码如下: totalCount = '100' totalPage = int(totalCount)/20 Va...

python装饰器decorator介绍

一、装饰器decorator decorator设计模式允许动态地对现有的对象或函数包装以至于修改现有的职责和行为,简单地讲用来动态地扩展现有的功能。其实也就是其他语言中的AOP的概念...