吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 4135|回复: 53
收起左侧

[Python 原创] python对多文件夹图片压缩,成品+源码

  [复制链接]
矢岛舞美 发表于 2023-5-30 15:15
本帖最后由 矢岛舞美 于 2023-5-30 23:08 编辑

QQ截图20230530145214.jpg QQ图片20230530230735_看图王.jpg
python代码:
[Asm] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
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
import os
import sys
from PIL import Image
from concurrent.futures import ThreadPoolExecutor
import multiprocessing
 
def is_image_file(file):
    try:
        with Image.open(file) as img:
            return img.format in ['JPEG', 'PNG', 'BMP', 'GIF', 'TIFF']
    except IOError:
        return False
 
def compress_image(input_file):
    # 打开图像文件
    with Image.open(input_file) as img:
        # 获取图像的格式
        file_format = img.format
 
        try:
            # 保存压缩后的图像
            img.save(input_file, format=file_format, optimize=True)
        except OSError:
            # 如果遇到缺少 EXIF 信息的情况,重新保存图像
            img.save(input_file, format=file_format, optimize=True, exif=b'')
 
    print(f"图像已成功压缩: {input_file}")
 
def compress_images_in_folders(thread_count):
    image_files = []
 
    # 遍历文件夹,找到所有图像文件
    for root, _, files in os.walk(os.getcwd()):
        for file in files:
            input_file = os.path.join(root, file)
 
            # 检查文件扩展名
            _, ext = os.path.splitext(input_file)
            if ext.lower() in ['.jpg', '.jpeg', '.png', '.bmp', '.gif', '.tiff']:
                if is_image_file(input_file):
                    image_files.append(input_file)
 
    # 使用线程池进行图像压缩
    with ThreadPoolExecutor(max_workers=thread_count) as executor:
        executor.map(compress_image, image_files)
 
if __name__ == "__main__":
    thread_count = multiprocessing.cpu_count()
    print(f"使用 {thread_count} 个线程进行图像压缩")
    compress_images_in_folders(thread_count)


成品地址:https://wwqb.lanzout.com/iGGT00xpr0ub   密码:2big

使用方法:放到要处理的文件夹,双击即可运行,比如:A文件夹下有B、C、D、E、F文件夹,将软件放到A目录下,即可对BCDEF下的图片进行压缩并对图片进行原位替换。
QQ截图20230530145126.jpg

免费评分

参与人数 8吾爱币 +12 热心值 +6 收起 理由
bdjshh + 1 谢谢分享
LHTHL + 1 谢谢@Thanks!
wh2510 + 1 可以指定范围大小吗,大佬
林逸致 + 1 + 1 谢谢@Thanks!
jun52pojiezhu + 1 谢谢@Thanks!
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
wanfon + 1 + 1 热心回复!
吾爱莫若 + 1 + 1 神器

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| 矢岛舞美 发表于 2023-5-30 22:37
Siono01 发表于 2023-5-30 17:48
谢谢分享。 顺便问一下这个可以压缩tga格式的嘛 这种压缩会不会损坏原始的的画质和质量的啊谢谢

那种十多M的图压缩之后看不出有啥损失,几百k的压到几十k会肉眼可见的有画质损失,我主要是压缩一些写真图,还有就是放在网站上的图
liaoyikai100 发表于 2023-6-27 14:10
[C] 纯文本查看 复制代码
01
02
03
04
05
06
07
08
09
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
54
55
56
57
58
59
60
61
62
63
64
65
66
import os
from PIL import Image
from concurrent.futures import ThreadPoolExecutor
import multiprocessing
 
 
def is_image_file(file):
    try:
        with Image.open(file) as img:
            return img.format in ['JPEG', 'PNG', 'BMP', 'GIF', 'TIFF']
    except IOError:
        return False
 
 
def compress_image(input_file, output_file):
    # 打开图像文件
    with Image.open(input_file) as img:
        # 获取图像的格式
        file_format = img.format
 
        try:
            # 保存压缩后的图像
            img.save(output_file, format=file_format, optimize=True)
        except OSError:
            # 如果遇到缺少 EXIF 信息的情况,重新保存图像
            img.save(output_file, format=file_format, optimize=True, exif=b'')
 
    print(f"图像已成功压缩: {output_file}")
 
 
def compress_images_in_folders(thread_count):
    # 获取用户输入的文件夹路径
    folder_path = input("请输入要压缩图像文件所在的文件夹路径:")
 
    # 遍历文件夹,找到所有图像文件
    image_files = []
    for root, _, files in os.walk(folder_path):
        for file in files:
            input_file = os.path.join(root, file)
 
            # 检查文件扩展名
            _, ext = os.path.splitext(input_file)
            if ext.lower() in ['.jpg', '.jpeg', '.png', '.bmp', '.gif', '.tiff']:
                if is_image_file(input_file):
                    image_files.append(input_file)
 
    # 使用线程池进行图像压缩
    with ThreadPoolExecutor(max_workers=thread_count) as executor:
        for input_file in image_files:
            # 生成压缩后的文件名
            output_file = os.path.splitext(input_file)[0] + "_compressed" + os.path.splitext(input_file)[1]
 
            # 检查是否已存在同名文件
            if os.path.exists(output_file):
                i = 1
                while os.path.exists(output_file):
                    output_file = os.path.splitext(input_file)[0] + f"_compressed_{i}" + os.path.splitext(input_file)[1]
                    i += 1
 
            executor.submit(compress_image, input_file, output_file)
 
 
if __name__ == "__main__":
    thread_count = multiprocessing.cpu_count()
    print(f"使用 {thread_count} 个线程进行图像压缩")
    compress_images_in_folders(thread_count)


更新一版手动选择路径并不替换源文件
Siono01 发表于 2023-5-30 17:48
谢谢分享。 顺便问一下这个可以压缩tga格式的嘛 这种压缩会不会损坏原始的的画质和质量的啊谢谢
111wdw 发表于 2023-5-30 18:19
这个软件很强大,值得收藏
jmsdqwl 发表于 2023-5-30 18:30
   py 不会 php会
ppplp 发表于 2023-5-30 21:11
听起来好像很牛Q的样子
Alice27 发表于 2023-5-30 22:10
谢谢分享,已收藏,辛苦啦
dcyxiaoxue 发表于 2023-5-31 07:26
辛苦了    收藏一下
nccdap 发表于 2023-5-31 08:59
保存了,研究学习
bjlaoge 发表于 2023-5-31 11:22
不错,很好用
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2025-5-17 20:15

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表