脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python 批量压缩图片的脚本

python 批量压缩图片的脚本

2021-11-19 13:54Mario-Hero Python

用Python编写的批量压缩图片的脚本,可以自定义压缩质量,有批量图片压缩需求的朋友可以直接拿来用

简介

用python批量压缩图片,把文件夹或图片直接拖入即可

需要 needs

python 3

pillow (用pip install pillow来安装即可)

用法 usage

把文件夹或图片直接拖入即可。如果拖入的是文件夹,则会遍历子文件夹把所有图片都压缩了。

注意,压缩后的文件会直接替换原来的文件,文件名不变,尺寸不变,只改变压缩质量。

文件的开头有两个变量:

size_cut = 4 表示大于4mb的图片都会进行压缩

quality = 90 表示压缩质量90,这个质量基本人眼是看不出来啥差距的,而且很多原先10m的图能压缩一半。80以下的质量大概就不太行了。

代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/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")

实现效果

python 批量压缩图片的脚本

压缩后大小

python 批量压缩图片的脚本

另外一种图片压缩实现方式

同样自动遍历目录下的图片

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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

以上就是python 批量压缩图片的脚本的详细内容,更多关于python 批量压缩图片的资料请关注服务器之家其它相关文章!

原文链接:https://github.com/Mario-Hero/PicCompress

延伸 · 阅读

精彩推荐