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使用urllib2获取网络资源实例讲解
Dec 02 Python
Python下的常用下载安装工具pip的安装方法
Nov 13 Python
Python采用Django制作简易的知乎日报API
Aug 03 Python
Python numpy实现二维数组和一维数组拼接的方法
Jun 05 Python
mac安装pytorch及系统的numpy更新方法
Jul 26 Python
python实现websocket的客户端压力测试
Jun 25 Python
Python turtle库绘制菱形的3种方式小结
Nov 23 Python
python 实现矩阵按对角线打印
Nov 29 Python
python双向链表原理与实现方法详解
Dec 03 Python
基于Python执行dos命令并获取输出的结果
Dec 30 Python
Python HTMLTestRunner可视化报告实现过程解析
Apr 10 Python
python实现扑克牌交互式界面发牌程序
Apr 22 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
window+nginx+php环境配置 附配置搭配说明
2010/12/29 PHP
PHP实现自动识别Restful API的返回内容类型
2015/02/07 PHP
php自定义函数实现JS的escape的方法示例
2016/07/07 PHP
常用PHP封装分页工具类
2017/01/14 PHP
浅析PHP7的多进程及实例源码
2019/04/14 PHP
javascript 面向对象的JavaScript类
2010/05/04 Javascript
jquery封装的对话框简单实现
2013/07/21 Javascript
基于js与flash实现的网站flv视频播放插件代码
2014/10/14 Javascript
不使用ajax实现无刷新提交表单
2014/12/21 Javascript
jQuery实现感应鼠标动画效果自动伸长的输入框实例
2015/02/24 Javascript
浅谈被jQuery抛弃的函数及替代函数
2015/05/03 Javascript
jQuery消息提示框插件Tipso
2015/05/04 Javascript
JavaScript实现的多种鼠标拖放效果
2015/11/03 Javascript
BootStrap glyphicons 字体图标实现方法
2016/05/01 Javascript
js中遍历Map对象的方法
2016/07/27 Javascript
详解自动生成博客目录案例
2016/12/09 Javascript
require.js与bootstrap结合实现简单的页面登录和页面跳转功能
2017/05/12 Javascript
详解Vue中过度动画效果应用
2017/05/25 Javascript
Vue通过阿里云oss的url连接直接下载文件并修改文件名的方法
2020/12/25 Vue.js
sqlalchemy对象转dict的示例
2014/04/22 Python
python制作企业邮箱的爆破脚本
2016/10/05 Python
遗传算法之Python实现代码
2017/10/10 Python
Python openpyxl 遍历所有sheet 查找特定字符串的方法
2018/12/10 Python
浅谈pymysql查询语句中带有in时传递参数的问题
2020/06/05 Python
css3简单练习实现遨游浏览器logo的绘制
2013/01/30 HTML / CSS
Aerosoles爱柔仕官网:美国舒软女鞋品牌
2017/07/17 全球购物
捷克体育用品购物网站:D-sport
2017/12/28 全球购物
英国文胸专家:AmpleBosom.com
2018/02/06 全球购物
学校安全责任书范本
2014/07/23 职场文书
县政府班子个人对照检查材料
2014/10/05 职场文书
房地产公司财务总监岗位职责
2015/04/03 职场文书
涨价通知怎么写
2015/04/23 职场文书
纪录片信仰观后感
2015/06/08 职场文书
中秋节感想
2015/08/10 职场文书
八年级地理课件资料及考点知识分享
2019/08/30 职场文书
win10如何开启ahci模式?win10开启ahci模式详细操作教程
2022/07/23 数码科技