python实现图片批量压缩程序


Posted in Python onJuly 23, 2018

 本文实例为大家分享了python实现图片批量压缩程序的具体代码,供大家参考,具体内容如下

说明

  • 运行环境:Win10 Pycharm
  • 程序没有用到面向对象编程方法,只是简单的面向过程设计
  • 用到的模块:PIL、os、sys
  • 使用方法: 在Pycharm的terminal中输入”python xxx.py source_dir dest_dir”就可以把source_dir中的图片文件进行压缩并保存到dest_dir中

源码

from PIL import Image
import os
import sys

# 定义可以识别的图片文件类型,可以自行扩充
valid_file_type = ['.jpg', '.png']
# 定义压缩比,数值越大,压缩越小
SIZE_normal = 1.0
SIZE_small = 1.5
SIZE_more_small = 2.0


def make_directory(directory):
  """创建目录"""
  os.makedirs(directory)


def directory_exists(directory):
  """判断目录是否存在"""
  if os.path.exists(directory):
    return True
  else:
    return False


def list_img_file(directory):
  """列出目录下所有文件,并筛选出图片文件列表返回"""
  old_list = os.listdir(directory)
  # print old_list
  new_list = []
  for filename in old_list:
    if os.path.isfile(filename):
      f, e = os.path.splitext(filename)
      if e in valid_file_type:
        new_list.append(filename)
      else:
        pass
    else:
      pass
  # print new_list
  return new_list


def print_help():
  print """
  This program helps compress many image files
  you can choose which scale you want to compress your img(jpg/png/etc)
  1) normal compress(4M to 1M around)
  2) small compress(4M to 500K around)
  3) smaller compress(4M to 300K around)
  """


def compress(choose, des_dir, file_list):
  """压缩算法,img.thumbnail对图片进行压缩,还可以改变宽高数值进行压缩"""
  if choose == '1':
    scale = SIZE_normal
  if choose == '2':
    scale = SIZE_small
  if choose == '3':
    scale = SIZE_more_small
  for infile in file_list:
    img = Image.open(infile)
    # size_of_file = os.path.getsize(infile)
    w, h = img.size
    img.thumbnail((int(w/scale), int(h/scale)))
    img.save(des_dir + '/' + infile)


if __name__ == "__main__":
  src_dir, des_dir = sys.argv[1], sys.argv[2]
  if directory_exists(src_dir):
    if not directory_exists(des_dir):
      make_directory(des_dir)
    # business logic
    file_list = list_img_file(src_dir)
    # print file_list
    if file_list:
      print_help()
      choose = raw_input("enter your choice:")
      compress(choose, des_dir, file_list)
    else:
      pass
  else:
    print "source directory not exist!"

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python里使用正则的findall函数的实例详解
Oct 19 Python
django表单实现下拉框的示例讲解
May 29 Python
对Tensorflow中的矩阵运算函数详解
Jul 27 Python
浅述python中深浅拷贝原理
Sep 18 Python
使用coverage统计python web项目代码覆盖率的方法详解
Aug 05 Python
Python 自动登录淘宝并保存登录信息的方法
Sep 04 Python
python基于socket实现的UDP及TCP通讯功能示例
Nov 01 Python
浅谈Python类中的self到底是干啥的
Nov 11 Python
使用jupyter notebook运行python和R的步骤
Aug 13 Python
python实现人工蜂群算法
Sep 18 Python
Selenium执行完毕未关闭chromedriver/geckodriver进程的解决办法(java版+python版)
Dec 07 Python
python绘制雷达图实例讲解
Jan 03 Python
python中的插值 scipy-interp的实现代码
Jul 23 #Python
Flask框架URL管理操作示例【基于@app.route】
Jul 23 #Python
python中的turtle库函数简单使用教程
Jul 23 #Python
Flask框架配置与调试操作示例
Jul 23 #Python
python实现时间o(1)的最小栈的实例代码
Jul 23 #Python
Flask框架Flask-Principal基本用法实例分析
Jul 23 #Python
Flask框架Flask-Login用法分析
Jul 23 #Python
You might like
php imagecreatetruecolor 创建高清和透明图片代码小结
2010/05/15 PHP
PHP中文竖排转换实现方法
2015/10/23 PHP
PHP实现的限制IP投票程序IP来源分析
2016/05/04 PHP
解析PHP之提取多维数组指定列的方法
2017/01/03 PHP
降低PHP Redis内存占用
2017/03/23 PHP
php获取微信共享收货地址的方法
2017/12/21 PHP
PHP中一个有趣的preg_replace函数详解
2018/08/15 PHP
详解PHP队列的实现
2019/03/14 PHP
javascript URL锚点取值方法
2009/02/25 Javascript
javascript弹出层输入框(示例代码)
2013/12/11 Javascript
js 时间格式与时间戳的相互转换示例代码
2013/12/25 Javascript
JavaScript二维数组实现的省市联动菜单
2014/05/08 Javascript
JS遍历Json字符串中键值对先转成JSON对象再遍历
2014/08/15 Javascript
JS辨别访问浏览器判断是android还是ios系统
2014/08/19 Javascript
详解javascript实现自定义事件
2016/01/19 Javascript
微信小程序  简单实例(阅读器)的实例开发
2016/09/29 Javascript
JavaScript正则替换HTML标签功能示例
2017/03/02 Javascript
ajax实现加载页面、删除、查看详细信息 bootstrap美化页面!
2017/03/14 Javascript
Angular指令之restict匹配模式的详解
2017/07/27 Javascript
vue微信分享的实现(在当前页面分享其他页面)
2019/04/16 Javascript
微信小程序图片左右摆动效果详解
2019/07/13 Javascript
举例讲解Python面向对象编程中类的继承
2016/06/17 Python
Python利用Beautiful Soup模块创建对象详解
2017/03/27 Python
python 3.5下xadmin的使用及修复源码bug
2017/05/10 Python
Python读取mat文件,并转为csv文件的实例
2018/07/04 Python
python去掉 unicode 字符串前面的u方法
2018/10/21 Python
python 把列表转化为字符串的方法
2018/10/23 Python
python爬虫看看虎牙女主播中谁最“顶”步骤详解
2020/12/01 Python
python 爬虫请求模块requests详解
2020/12/04 Python
浅析两列自适应布局的3种思路
2016/05/03 HTML / CSS
业务代表的岗位职责
2013/11/16 职场文书
招聘单位介绍信
2014/01/14 职场文书
优秀教师主要事迹
2014/02/01 职场文书
党的生日活动方案
2014/08/15 职场文书
建议书范文
2015/02/05 职场文书
红高粱观后感
2015/06/10 职场文书