1
+ #!/usr/bin/env python3
2
+ """
3
+ ImageConverter 命令行菜单系统 v1.0
4
+ 功能:提供用户友好的图像转换界面,无需修改原始转换脚本
5
+ """
6
+
1
7
import os
8
+ import subprocess
9
+ import sys
10
+ from pathlib import Path
11
+
12
+ # 原始转换脚本名称
13
+ CONVERTER_SCRIPT = "image_converter.py"
14
+
15
+
16
+ # 颜色定义 (ANSI 转义序列)
17
+ class Colors :
18
+ HEADER = '\033 [95m'
19
+ BLUE = '\033 [94m'
20
+ CYAN = '\033 [96m'
21
+ GREEN = '\033 [92m'
22
+ YELLOW = '\033 [93m'
23
+ RED = '\033 [91m'
24
+ ENDC = '\033 [0m'
25
+ BOLD = '\033 [1m'
26
+ UNDERLINE = '\033 [4m'
27
+
28
+
29
+ def clear_screen ():
30
+ """清屏函数,跨平台支持"""
31
+ os .system ('cls' if os .name == 'nt' else 'clear' )
32
+
33
+
34
+ def display_banner ():
35
+ """显示程序横幅"""
36
+ print (f"{ Colors .BLUE } { '=' * 60 } " )
37
+ print (f"{ Colors .BOLD } ImageConverter 命令行菜单系统 v1.0{ Colors .ENDC } " )
38
+ print (f"{ '=' * 60 } { Colors .ENDC } " )
39
+ print (f"{ Colors .CYAN } 此菜单系统提供对 { CONVERTER_SCRIPT } 的友好访问" )
40
+ print (f"无需修改原始转换脚本{ Colors .ENDC } \n " )
41
+
42
+
43
+ def validate_script ():
44
+ """验证转换脚本是否存在"""
45
+ if not Path (CONVERTER_SCRIPT ).is_file ():
46
+ print (f"{ Colors .RED } 错误: 未找到转换脚本 { CONVERTER_SCRIPT } " )
47
+ print ("请确保此菜单脚本与转换脚本在同一目录下{Colors.ENDC}" )
48
+ sys .exit (1 )
49
+
50
+
51
+ def run_converter (command ):
52
+ """运行转换器脚本"""
53
+ try :
54
+ clear_screen ()
55
+ print (f"{ Colors .YELLOW } 执行命令: python { CONVERTER_SCRIPT } { ' ' .join (command )} { Colors .ENDC } \n " )
56
+ result = subprocess .run (
57
+ [sys .executable , CONVERTER_SCRIPT ] + command ,
58
+ check = True
59
+ )
60
+ return result .returncode == 0
61
+ except subprocess .CalledProcessError as e :
62
+ print (f"{ Colors .RED } 转换出错: { e } { Colors .ENDC } " )
63
+ return False
64
+ except KeyboardInterrupt :
65
+ print (f"\n { Colors .YELLOW } 操作已取消{ Colors .ENDC } " )
66
+ return False
67
+
68
+
69
+ def get_input_folder ():
70
+ """获取用户输入文件夹路径"""
71
+ default_input = "./input"
72
+ print (f"\n { Colors .CYAN } 请输入输入文件夹路径 (默认为 '{ default_input } '):" )
73
+ path = input ("> " ).strip ()
74
+ return path if path else default_input
75
+
76
+
77
+ def get_output_folder ():
78
+ """获取用户输出文件夹路径"""
79
+ default_output = "./output"
80
+ print (f"\n { Colors .CYAN } 请输入输出文件夹路径 (默认为 '{ default_output } '):" )
81
+ path = input ("> " ).strip ()
82
+ return path if path else default_output
83
+
84
+
85
+ def get_quality ():
86
+ """获取质量设置"""
87
+ while True :
88
+ print (f"\n { Colors .CYAN } 请输入图像质量 (1-100, 默认为 85):" )
89
+ q = input ("> " ).strip ()
90
+ if not q :
91
+ return 85
92
+ try :
93
+ quality = int (q )
94
+ if 1 <= quality <= 100 :
95
+ return quality
96
+ print (f"{ Colors .RED } 错误: 质量值必须在 1-100 之间{ Colors .ENDC } " )
97
+ except ValueError :
98
+ print (f"{ Colors .RED } 错误: 请输入有效数字{ Colors .ENDC } " )
99
+
100
+
101
+ def to_webp_menu ():
102
+ """转换为WebP菜单"""
103
+ clear_screen ()
104
+ print (f"{ Colors .GREEN } { '=' * 30 } " )
105
+ print ("转换为WebP格式" )
106
+ print (f"{ '=' * 30 } { Colors .ENDC } " )
107
+
108
+ input_folder = get_input_folder ()
109
+ output_folder = get_output_folder ()
110
+ quality = get_quality ()
111
+
112
+ command = [
113
+ "to_webp" ,
114
+ "-i" , input_folder ,
115
+ "-o" , output_folder ,
116
+ "-q" , str (quality )
117
+ ]
118
+
119
+ if run_converter (command ):
120
+ print (f"\n { Colors .GREEN } 转换完成! 输出目录: { output_folder } { Colors .ENDC } " )
121
+ input ("\n 按回车键返回主菜单..." )
122
+
123
+
124
+ def from_webp_menu ():
125
+ """从WebP转换菜单"""
126
+ clear_screen ()
127
+ print (f"{ Colors .GREEN } { '=' * 30 } " )
128
+ print ("从WebP转换格式" )
129
+ print (f"{ '=' * 30 } { Colors .ENDC } " )
130
+
131
+ input_folder = get_input_folder ()
132
+ output_folder = get_output_folder ()
133
+
134
+ # 格式选择
135
+ print (f"\n { Colors .CYAN } 请选择输出格式:" )
136
+ print ("1) PNG (默认)" )
137
+ print ("2) JPG/JPEG" )
138
+ choice = input ("> " ).strip ()
139
+
140
+ format = "png"
141
+ if choice == "2" :
142
+ format = "jpg"
143
+
144
+ quality = get_quality ()
145
+
146
+ command = [
147
+ "from_webp" ,
148
+ "-i" , input_folder ,
149
+ "-o" , output_folder ,
150
+ "-f" , format ,
151
+ "-q" , str (quality )
152
+ ]
153
+
154
+ if run_converter (command ):
155
+ print (f"\n { Colors .GREEN } 转换完成! 输出目录: { output_folder } { Colors .ENDC } " )
156
+ input ("\n 按回车键返回主菜单..." )
157
+
158
+
159
+ def main_menu ():
160
+ """主菜单"""
161
+ validate_script ()
162
+
163
+ while True :
164
+ clear_screen ()
165
+ display_banner ()
166
+
167
+ print (f"{ Colors .YELLOW } 请选择操作:{ Colors .ENDC } " )
168
+ print (f"{ Colors .CYAN } 1) 将图像转换为WebP格式" )
169
+ print ("2) 将WebP转换为其他格式" )
170
+ print ("3) 退出程序" )
171
+ print (f"{ Colors .YELLOW } { '-' * 30 } { Colors .ENDC } " )
172
+
173
+ choice = input ("> " ).strip ()
174
+
175
+ if choice == "1" :
176
+ to_webp_menu ()
177
+ elif choice == "2" :
178
+ from_webp_menu ()
179
+ elif choice == "3" :
180
+ print (f"\n { Colors .BLUE } 感谢使用ImageConverter菜单系统!{ Colors .ENDC } " )
181
+ sys .exit (0 )
182
+ else :
183
+ print (f"{ Colors .RED } 无效选择,请重新输入{ Colors .ENDC } " )
184
+ input ("按回车键继续..." )
2
185
3
186
4
- command = "from_webp -q 100"
5
- os .system ("python3 image_converter.py " + command )
187
+ if __name__ == "__main__" :
188
+ try :
189
+ main_menu ()
190
+ except KeyboardInterrupt :
191
+ print (f"\n { Colors .YELLOW } 程序已终止{ Colors .ENDC } " )
192
+ sys .exit (0 )
0 commit comments