python实现图片批量压缩程序

作者:lannooooooooooo 时间:2021-11-27 03:50:11 

 本文实例为大家分享了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!"

来源:https://blog.csdn.net/qq_22187919/article/details/52551526

标签:python,批量压缩,图片压缩
0
投稿

猜你喜欢

  • js控制表单不能输入空格的小例子

    2013-07-21 11:58:51
  • Python中JsonPath提取器和正则提取器

    2022-08-27 14:12:20
  • python实现XML解析的方法解析

    2021-08-20 01:58:58
  • 让XML在ASP中发挥其长处

    2008-01-16 19:07:00
  • django模型查询操作的实现

    2023-03-31 04:01:04
  • 学生信息管理系统python版

    2023-10-17 07:32:34
  • 动态SQL中返回数值的实现代码

    2012-01-05 18:53:54
  • matplotlib quiver箭图绘制案例

    2023-07-13 10:04:11
  • 如何用Python来理一理红楼梦里的那些关系

    2023-03-28 08:56:31
  • php的RSA加密解密算法原理与用法分析

    2023-07-13 11:27:14
  • SQL语句练习实例之七 剔除不需要的记录行

    2011-11-03 16:50:51
  • python计算机视觉opencv图像金字塔轮廓及模板匹配

    2021-10-10 09:15:33
  • 常用的数据库备份类型有哪些?

    2009-11-01 13:02:00
  • python遍历文件夹下所有excel文件

    2023-10-26 17:57:12
  • Python使用Matplotlib模块时坐标轴标题中文及各种特殊符号显示方法

    2021-03-12 03:38:27
  • Python中标准库OS的常用方法总结大全

    2021-04-29 06:43:41
  • Python内置数据类型中的集合详解

    2022-07-13 04:06:38
  • Pycharm最新激活码2019(推荐)

    2023-07-24 20:30:26
  • 深入解析Python编程中JSON模块的使用

    2022-11-30 00:21:27
  • Go语言 go程释放操作(退出/销毁)

    2023-09-17 22:03:42
  • asp之家 网络编程 m.aspxhome.com