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卸载模块的方法汇总
Jun 07 Python
Python使用cookielib模块操作cookie的实例教程
Jul 12 Python
python的pdb调试命令的命令整理及实例
Jul 12 Python
python实现数据图表
Jul 29 Python
python 地图经纬度转换、纠偏的实例代码
Aug 06 Python
Python实现图片转字符画的代码实例
Feb 22 Python
Django项目中添加ldap登陆认证功能的实现
Apr 04 Python
Python模块、包(Package)概念与用法分析
May 31 Python
python issubclass 和 isinstance函数
Jul 25 Python
python自动化测试三部曲之unittest框架的实现
Oct 07 Python
Python实现冒泡排序算法的完整实例
Nov 04 Python
python xlsxwriter模块的使用
Dec 24 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
SONY SRF-M100的电路分析
2021/03/02 无线电
CURL的学习和应用(附多线程实现)
2013/06/03 PHP
php文件夹的创建与删除方法
2015/01/24 PHP
php获取、检查类名、函数名、方法名的函数方法
2015/06/25 PHP
Java中final关键字详解
2015/08/10 PHP
PHP substr()函数参数解释及用法讲解
2017/11/23 PHP
php的优点总结 php有哪些优点
2019/07/19 PHP
Javascript & DHTML 实例编程(教程)(三)初级实例篇1—上传文件控件实例
2007/06/02 Javascript
Js 代码中,ajax请求地址后加随机数防止浏览器缓存的原因
2013/05/07 Javascript
JS计算网页停留时间代码
2014/04/28 Javascript
JQuery操作textarea,input,select,checkbox方法
2015/09/02 Javascript
实例讲解使用原生JavaScript处理AJAX请求的方法
2016/05/10 Javascript
Node.js的环境安装配置(使用nvm方式)
2016/10/11 Javascript
分享javascript、jquery实用代码段
2016/10/20 Javascript
jQuery读取XML文件的方法示例
2017/02/03 Javascript
基于vue 添加axios组件,解决post传参数为null的问题
2018/03/05 Javascript
JS实现匀速与减速缓慢运动的动画效果封装示例
2018/08/27 Javascript
JS中数据结构之栈
2019/01/01 Javascript
使用ThinkJs搭建微信中控服务的实现方法
2019/08/08 Javascript
JavaScript中的相等操作符使用详解
2019/12/21 Javascript
Node.js API详解之 querystring用法实例分析
2020/04/29 Javascript
深入理解Python爬虫代理池服务
2018/02/28 Python
python用plt画图时,cmp设置方法
2018/12/13 Python
anaconda如何查看并管理python环境
2019/07/05 Python
Django Form 实时从数据库中获取数据的操作方法
2019/07/25 Python
pandas读取csv文件提示不存在的解决方法及原因分析
2020/04/21 Python
keras:model.compile损失函数的用法
2020/07/01 Python
在 Python 中使用 MQTT的方法
2020/08/18 Python
python 如何实现遗传算法
2020/09/22 Python
如何利用python读取micaps文件详解
2020/10/18 Python
HTML5中视频音频的使用详解
2017/07/07 HTML / CSS
技术总监个人的自我评价范文
2013/12/18 职场文书
舞蹈比赛获奖感言
2014/02/04 职场文书
实验教师岗位职责
2014/02/13 职场文书
秋天的怀念教学反思
2014/04/28 职场文书
聘任证明怎么写
2015/03/02 职场文书