python 批量压缩图片的脚本
简介
用Python批量压缩图片,把文件夹或图片直接拖入即可
需要 Needs
Python 3
Pillow (用pip install pillow来安装即可)
用法 Usage
把文件夹或图片直接拖入即可。如果拖入的是文件夹,则会遍历子文件夹把所有图片都压缩了。
注意,压缩后的文件会直接替换原来的文件,文件名不变,尺寸不变,只改变压缩质量。
文件的开头有两个变量:
SIZE_CUT = 4 表示大于4MB的图片都会进行压缩
QUALITY = 90 表示压缩质量90,这个质量基本人眼是看不出来啥差距的,而且很多原先10M的图能压缩一半。80以下的质量大概就不太行了。
代码
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
# Created by Mario Chen, 01.04.2021, Shenzhen
# My Github site: https://github.com/Mario-Hero
import sys
import os
from PIL import Image
SIZE_CUT = 4 # picture over this size should be compressed. Units: MB
QUALITY = 90 # 90 is good, this number should not be smaller than 80.
def isPic(name):
namelower = name.lower()
return namelower.endswith("jpeg") or namelower.endswith("jpg") or namelower.endswith("png")
def compressImg(file):
#print("The size of", file, "is: ", os.path.getsize(file))
im = Image.open(file)
im.save(file, quality=QUALITY)
def compress(folder):
try:
if os.path.isdir(folder):
print(folder)
file_list = os.listdir(folder)
for file in file_list:
if os.path.isdir(folder+"/"+file):
#print(folder +"/"+ file)
compress(folder +"/"+file)
else:
if isPic(file):
if os.path.getsize(folder + "/" + file) > (SIZE_CUT * 1024 * 1024):
compressImg(folder + "/" + file)
print(file)
else:
if isPic(folder):
if os.path.getsize(folder) > (SIZE_CUT * 1024 * 1024):
compressImg(folder)
except BaseException:
return
if __name__ == '__main__':
for folder in sys.argv:
#print(folder)
compress(folder)
print("Finish.")
#os.system("pause")
实现效果
压缩后大小
另外一种图片压缩实现方式
同样自动遍历目录下的图片
import os
from PIL import Image
import threading,time
def imgToProgressive(path):
if not path.split('.')[-1:][0] in ['png','jpg','jpeg']: #if path isn't a image file,return
return
if os.path.isdir(path):
return
##########transform img to progressive
img = Image.open(path)
destination = path.split('.')[:-1][0]+'_destination.'+path.split('.')[-1:][0]
try:
print(path.split('\\')[-1:][0],'开始转换图片')
img.save(destination, "JPEG", quality=80, optimize=True, progressive=True) #转换就是直接另存为
print(path.split('\\')[-1:][0],'转换完毕')
except IOError:
PIL.ImageFile.MAXBLOCK = img.size[0] * img.size[1]
img.save(destination, "JPEG", quality=80, optimize=True, progressive=True)
print(path.split('\\')[-1:][0],'转换完毕')
print('开始重命名文件')
os.remove(path)
os.rename(destination,path)
for d,_,fl in os.walk(os.getcwd()): #遍历目录下所有文件
for f in fl:
try:
imgToProgressive(d+'\\'+f)
except:
pass
(资源库 www.zyku.net)
原文链接:https://github.com/Mario-Hero/PicCompress
上一篇:matplotlib共享坐标轴的实现(X或Y坐标轴)
栏 目:Python教程
下一篇:浅谈tensorflow语义分割api的使用(deeplab训练cityscapes)
本文标题:python 批量压缩图片的脚本
本文地址:https://www.zyku.net/python/9638.html
您可能感兴趣的文章
- 01-25基于Python中request请求得到的respon
- 07-05Linux skill命令
- 01-11看看3D卫星街景-看看3D卫星街景应用软
- 09-27vivo手机关闭长按识别图片教程介绍
- 01-11开鲁网-开鲁网应用软件功能介绍
- 02-08用python实现一个无界面的小型图书管
- 12-27义乌货源网-义乌货源网应用软件功能介
- 03-12帝国CMS如何调用相关文章
- 09-14小米平板5pro启用杜比全景声教程分享
- 04-08div去除点击出现蓝框的方法
- 10-12帝国cms网站安全设置 让你的网站更安
- 02-26帝国CMS忘记后台管理员帐号怎么办?
- 01-12小仙女助手-小仙女助手应用软件功能介
- 01-13iphone13怎么设置画中画
- 08-10快手直播电视投屏教程
- 01-12SOVS相机-SOVS相机应用软件功能介绍
- 01-11棒棒哒音乐-棒棒哒音乐应用软件功能介
- 02-20Windows 7中打开IIS的远程访问
- 02-22黑鲨4pro系统导航方式更改方法
- 11-10支付宝二十四节气壁纸在哪里领
最近更新
阅读排行
猜你喜欢
- 01-25Python如何配置环境变量详解
- 01-02录屏软件精灵-录屏软件精灵应用软件功
- 04-03CentOS 7系统下安装ftp服务器的全过程
- 08-02Discuz!升级后Ucenter无法登录的解决
- 12-27华为mate40pro怎么取消应用安全检测
- 04-26华为nova8设置充电气泡界面教程
- 08-13支付宝取消淘宝100元以下免密支付技巧
- 03-07网站被挂木马与777权限的奥妙
- 02-20使用notepad运行php,python的图文设置
- 09-28realmegtneo2显示流量怎么开