Delete webp.py
Browse files
webp.py
DELETED
@@ -1,116 +0,0 @@
|
|
1 |
-
import os
|
2 |
-
import argparse
|
3 |
-
from PIL import Image
|
4 |
-
from tqdm import tqdm
|
5 |
-
from concurrent.futures import ThreadPoolExecutor
|
6 |
-
from functools import partial
|
7 |
-
|
8 |
-
def convert_single_image(filename, input_dir, output_dir, quality=95):
|
9 |
-
"""
|
10 |
-
转换单个图片为WebP格式
|
11 |
-
|
12 |
-
Args:
|
13 |
-
filename: 图片文件名
|
14 |
-
input_dir: 输入目录路径
|
15 |
-
output_dir: 输出目录路径
|
16 |
-
quality: WebP压缩质量(1-100)
|
17 |
-
"""
|
18 |
-
input_path = os.path.join(input_dir, filename)
|
19 |
-
output_filename = os.path.splitext(filename)[0] + '.webp'
|
20 |
-
output_path = os.path.join(output_dir, output_filename)
|
21 |
-
|
22 |
-
# 检查输出文件是否已存在
|
23 |
-
if os.path.exists(output_path):
|
24 |
-
return True
|
25 |
-
|
26 |
-
try:
|
27 |
-
# 打开图片
|
28 |
-
image = Image.open(input_path)
|
29 |
-
|
30 |
-
# 转换为webp格式并保存
|
31 |
-
image.save(output_path, 'webp', quality=quality)
|
32 |
-
image.close()
|
33 |
-
|
34 |
-
return True
|
35 |
-
|
36 |
-
except Exception as e:
|
37 |
-
print(f"\n转换 {filename} 时出错: {str(e)}")
|
38 |
-
return False
|
39 |
-
|
40 |
-
def convert_to_webp(input_dir='downloaded_images', output_dir='webp_images', quality=95):
|
41 |
-
"""
|
42 |
-
批量转换图片为WebP格式
|
43 |
-
|
44 |
-
Args:
|
45 |
-
input_dir: 输入图片目录
|
46 |
-
output_dir: 输出WebP图片目录
|
47 |
-
quality: WebP压缩质量(1-100)
|
48 |
-
"""
|
49 |
-
print(f"\n开始转换图片到WebP格式...")
|
50 |
-
print(f"输入目录: {input_dir}")
|
51 |
-
print(f"输出目录: {output_dir}")
|
52 |
-
print(f"压缩质量: {quality}\n")
|
53 |
-
|
54 |
-
# 确保输入目录存在
|
55 |
-
if not os.path.exists(input_dir):
|
56 |
-
print(f"错误: 输入目录 {input_dir} 不存在!")
|
57 |
-
return
|
58 |
-
|
59 |
-
# 确保输出目录存在
|
60 |
-
if not os.path.exists(output_dir):
|
61 |
-
os.makedirs(output_dir)
|
62 |
-
print(f"已创建输出目录: {output_dir}")
|
63 |
-
|
64 |
-
# 获取所有支持的图片文件
|
65 |
-
supported_formats = ('.png', '.jpg', '.jpeg', '.bmp', '.gif')
|
66 |
-
image_files = [f for f in os.listdir(input_dir)
|
67 |
-
if os.path.isfile(os.path.join(input_dir, f))
|
68 |
-
and f.lower().endswith(supported_formats)]
|
69 |
-
|
70 |
-
if not image_files:
|
71 |
-
print(f"错误: 在 {input_dir} 中未找到支持的图片文件!")
|
72 |
-
print(f"支持的格式: {', '.join(supported_formats)}")
|
73 |
-
return
|
74 |
-
|
75 |
-
# 检查已转换的文件
|
76 |
-
existing_webp = {os.path.splitext(f)[0] for f in os.listdir(output_dir)
|
77 |
-
if f.lower().endswith('.webp')}
|
78 |
-
image_files = [f for f in image_files
|
79 |
-
if os.path.splitext(f)[0] not in existing_webp]
|
80 |
-
|
81 |
-
if not image_files:
|
82 |
-
print("所有文件已转换完成,无需重复转换!")
|
83 |
-
return
|
84 |
-
|
85 |
-
print(f"找到 {len(image_files)} 个新图片文件待转换")
|
86 |
-
|
87 |
-
# 使用线程池并行处理图片转换
|
88 |
-
success_count = 0
|
89 |
-
with ThreadPoolExecutor(max_workers=os.cpu_count()) as executor:
|
90 |
-
convert_func = partial(convert_single_image,
|
91 |
-
input_dir=input_dir,
|
92 |
-
output_dir=output_dir,
|
93 |
-
quality=quality)
|
94 |
-
results = list(tqdm(executor.map(convert_func, image_files),
|
95 |
-
total=len(image_files),
|
96 |
-
desc="转换进度"))
|
97 |
-
success_count = sum(results)
|
98 |
-
|
99 |
-
print(f"\n转换完成!")
|
100 |
-
print(f"成功: {success_count} 个文件")
|
101 |
-
print(f"失败: {len(image_files) - success_count} 个文件")
|
102 |
-
|
103 |
-
def main():
|
104 |
-
parser = argparse.ArgumentParser(description='批量转换图片为WebP格式')
|
105 |
-
parser.add_argument('-i', '--input', default='downloaded_images',
|
106 |
-
help='输入图片目录路径 (默认: downloaded_images)')
|
107 |
-
parser.add_argument('-o', '--output', default='webp_images',
|
108 |
-
help='输出WebP图片目录路径 (默认: webp_images)')
|
109 |
-
parser.add_argument('-q', '--quality', type=int, default=95,
|
110 |
-
help='WebP压缩质量 1-100 (默认: 95)')
|
111 |
-
args = parser.parse_args()
|
112 |
-
|
113 |
-
convert_to_webp(args.input, args.output, args.quality)
|
114 |
-
|
115 |
-
if __name__ == "__main__":
|
116 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|