批量调整图片尺寸的python脚本
图片尺寸太大,数量太多,不方便存储,怎么办?看看下边的这个Python代码你就知道啦。
这是一个网友写的,写这段python代码的原因是,因为像素一般都比较大,一张就十多M了,不方便放入移动设备存放,于是写个小脚本压缩一下。
python新手,写得不好,还请包涵。
相关文章推荐:如何用Python方法获取图片的准确尺寸
批量调整图片尺寸的python脚本,源码如下:
#coding:utf-8 #author:solu import sys, os from PIL import Image from optparse import OptionParser #遍历指定目录下的JPG图片,返回list def walk_dir(dir): image_list = [] for root, dirs, files in os.walk(dir): for name in files: ext = os.path.splitext(name)[1][1:] if (ext.lower() == 'jpg'): path = root + os.sep + name image_list.append(path) return image_list #保存图片,默认保存在图片目录下的thumb def resize_save(im, width, path): image_name = im.filename.split(os.sep)[-1] save_name = path + os.sep + image_name size = auto_resize(im, width) new_im = im.resize(size) print save_name new_im.save(save_name) #调整宽高 def auto_resize(im, width): size = im.size height = int(float(width) / size[0] * size[1]) return (int(width), height) if __name__ == '__main__': usage_msg = 'usage: %prog -p <image_path> -w <image_width>' parser = OptionParser(usage_msg) parser.add_option("-p","--path", dest = "image_path", help = u"存放相片的路径") parser.add_option("-w","--width", dest = "image_width", help = u"调整后的图片宽度(高度会自等比例缩放)") options, args = parser.parse_args() if not options.image_path or not options.image_width: parser.print_help() sys.exit(1) try: image_path = unicode(options.image_path, 'gbk') width = options.image_width #创建文件夹 save_path = image_path + os.sep + 'thumb' if (not os.path.exists(save_path)): os.mkdir(save_path) image_list = walk_dir(image_path) for path in image_list: im = Image.open(path) resize_save(im, width, save_path) except Exception,e: print('Error:',e) #www.iplaypy.com
玩蛇网文章,转载请注明出处和文章网址:https://www.iplaypy.com/code/scripts-shell/ss2296.html
相关文章 Recommend
- • python shell脚很调用时很耗内存怎么调整
- • 用python获取带图片的验证码怎么样
- • Python sqlalchemy批量数据插入优化注意事项有哪些
- • Flask响应内容为图片时怎么体现
- • python2.7配Django1.4GAE方面需要做什么调整
- • 支持url批量发post请求的python工具是什么?
- • Python for循环中用循环变量批量创建函数功能实现
- • python fabric批量管理多台server执行方法问题
- • python批量抓取二级域名标题的方法
- • python哪个组件最适合做图片文件管理
- • django:在打开admin页面的时候出错了(其它页面能
- • python将文字转图片发微博求思路
我要分享到:
必知PYTHON教程 Must Know PYTHON Tutorials
- • python 解释器
- • python idle
- • python dir函数
- • python 数据类型
- • python type函数
- • python 字符串
- • python 整型数字
- • python 列表
- • python 元组
- • python 字典
- • python 集合
- • python 变量
- • python print
- • python 函数
- • python 类定义
- • python import
- • python help
- • python open
- • python 异常处理
- • python 注释
- • python continue
- • python pass
- • python return
- • python global
- • python assert
- • python if语句
- • python break
- • python for循环
- • python while循环
- • python else/elif
- • lambda匿名函数
必知PYTHON模块 Must Know PYTHON Modules
- • os 模块
- • sys 模块
- • re 正则表达式
- • time 日期时间
- • pickle 持久化
- • random 随机
- • csv 模块
- • logging 日志
- • socket网络通信
- • json模块
- • urlparse 解析URL
- • urllib 模块
- • urllib2 模块
- • robotparser 解析
- • Cookie 模块
- • smtplib 邮件
- • Base64 编码
- • xmlrpclib客户端
- • string 文本
- • Queue 线程安全
- • math数学计算
- • linecache缓存
- • threading多线程
- • sqlite3数据库
- • gzip压缩解压
最新内容 NEWS
- • 绑定修改网卡绑定关系的python程序
- • 网友用python把IPv4地址变成LITNET-NAT64网段
- • 基于python Selenium的用户登录自动化测试
- • 用python代码科学上Google
- • python论坛自动签到用bs4模块
- • python北京地铁月支出简易计算器
- • 用python查找未注册的域名
- • Windows环境用Python备份MySQL脚本
- • 批量调整图片尺寸的python脚本
- • Python方法获取百度地图数据示例源码
图文精华 RECOMMEND
-
用python代码科学上Google
-
Windows环境用Python备份MySQL脚本
-
批量调整图片尺寸的python脚本
-
Python方法获取百度地图数据示例源
-
控制台进度自动刷新python方法源码
-
web.py能条件判断的页面执行计时方
热点文章 HOT
- web.py能条件判断的页面执行计时方法
- Windows环境用Python备份MySQL脚本
- 从糗事百科下载数据的python方法示例
- 基于python Selenium的用户登录自动化测试
- 解决Python2不支持datetime的json encode问题
- Python完成抓取并写入mysql库的方法
- 一个初学者练手的Python多线程实现下载的
- Python调chrome刷页面完成刷点击量操作