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绘图库Matplotlib的安装
Jul 03 Python
python用10行代码实现对黄色图片的检测功能
Aug 10 Python
Linux 下 Python 实现按任意键退出的实现方法
Sep 25 Python
python基于ID3思想的决策树
Jan 03 Python
python traceback捕获并打印异常的方法
Aug 31 Python
解决python中画图时x,y轴名称出现中文乱码的问题
Jan 29 Python
Python+opencv 实现图片文字的分割的方法示例
Jul 04 Python
Python 最强编辑器详细使用指南(PyCharm )
Sep 16 Python
导入tensorflow时报错:cannot import name 'abs'的解决
Oct 10 Python
使用Python的Turtle库绘制森林的实例
Dec 18 Python
基于python实现音乐播放器代码实例
Jul 01 Python
python的launcher用法知识点总结
Aug 07 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
dedecms 制作模板中使用的全局标记图文教程
2007/03/11 PHP
安装ImageMagick出现error while loading shared libraries的解决方法
2014/09/23 PHP
php生成html文件方法总结
2014/12/01 PHP
PHP8.0新功能之Match表达式的使用
2020/07/19 PHP
SyntaxHighlighter代码加色使用方法
2008/09/07 Javascript
简单实用jquery版三级联动select示例
2013/07/04 Javascript
Jquery.addClass始终无效原因分析
2013/09/08 Javascript
表单提交前触发函数返回true表单才会提交
2014/03/11 Javascript
jquery.form.js实现将form提交转为ajax方式提交的方法
2015/04/07 Javascript
js简单的点击返回顶部效果实现方法
2015/04/10 Javascript
使用 JavaScript 进行函数式编程 (一) 翻译
2015/10/02 Javascript
深入浅析NodeJs并发异步的回调处理
2015/12/21 NodeJs
非常酷炫的Bootstrap图片轮播动画
2016/05/27 Javascript
Highcharts 多个Y轴动态刷新数据的实现代码
2016/05/28 Javascript
js完整倒计时代码分享
2016/09/18 Javascript
jQuery实现鼠标悬停3d菜单展开动画效果
2017/01/19 Javascript
Angularjs自定义指令实现分页插件(DEMO)
2017/09/16 Javascript
JS实现利用两个队列表示一个栈的方法
2017/12/13 Javascript
使用ESLint禁止项目导入特定模块的方法步骤
2019/03/04 Javascript
新手如何快速理解js异步编程
2019/06/24 Javascript
JavaScript实现联动菜单特效
2020/01/07 Javascript
python中快速进行多个字符替换的方法小结
2016/12/15 Python
Python中存取文件的4种不同操作
2018/07/02 Python
浅谈Python3 numpy.ptp()最大值与最小值的差
2019/08/24 Python
pytorch实现mnist分类的示例讲解
2020/01/10 Python
Python  word实现读取及导出代码解析
2020/07/09 Python
canvas实现图片马赛克的示例代码
2018/03/26 HTML / CSS
护理专业推荐信
2013/11/07 职场文书
小学语文国培感言
2014/03/04 职场文书
婚礼司仪主持词
2014/03/14 职场文书
活动策划求职信模板
2014/04/21 职场文书
护理专业毕业生自荐信
2014/06/15 职场文书
学风建设演讲稿
2014/09/12 职场文书
首席执行官观后感
2015/06/03 职场文书
Python语言中的数据类型-序列
2022/02/24 Python
python缺失值填充方法示例代码
2022/12/24 Python