forked from leslievan/semi-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·251 lines (225 loc) · 8.32 KB
/
main.py
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import os
from tqdm import tqdm
from config import Layout, input_dir, config, font, bold_font, get_logo, white_margin_enable, white_margin_width, \
output_dir, quality, logo_enable, save_config, load_config
from image_container import ImageContainer
from image_processor import ImageProcessor, padding_image
from utils import get_file_list
id_to_name = {'Model': '相机机型', 'Make': '相机厂商', 'LensModel': '镜头型号', 'Param': '拍摄参数', 'Date': '拍摄时间',
'None': '无'}
s_line = '+' + '-' * 15 + '+' + '-' * 15 + '+'
id_to_loc = {'left_top': '左上文字', 'right_top': '右上文字', 'left_bottom': '左下文字', 'right_bottom': '右下文字'}
def parse_elem_value(element):
if element['name'] == 'Custom':
return '自定义字段 (' + (element['value'] if 'value' in element else '') + ')'
else:
return id_to_name.setdefault(element['name'], '值错误')
def print_current_setting():
"""
状态1:开始菜单
:return:
"""
global state
print(s_line)
print(' ' * 8 + '当前设置')
print(s_line)
print(' 【1】: 布局:{}'.format(config['layout']['type']))
print(' 【2】: Logo:{}'.format("显示" if config['logo']['enable'] else "不显示"))
print(' 【3】: 左上文字:{}'.format(parse_elem_value(config['layout']['elements']['left_top'])))
print(' 【4】: 左下文字:{}'.format(parse_elem_value(config['layout']['elements']['left_bottom'])))
print(' 【5】: 右上文字:{}'.format(parse_elem_value(config['layout']['elements']['right_top'])))
print(' 【6】: 右下文字:{}'.format(parse_elem_value(config['layout']['elements']['right_bottom'])))
print(s_line)
user_input = input('输入【y 或回车】按照当前设置开始处理图片,输入【数字】修改设置,输入【x】退出程序\n')
if user_input == 'y' or user_input == '':
state = 100
elif user_input == 'x' or user_input == 'X':
exit(0)
elif user_input.isdigit():
state = int(user_input)
def processing():
"""
状态1:处理图片
:return:
"""
global state
file_list = get_file_list(input_dir)
layout = Layout(config['layout'])
print('当前共有 {} 张图片待处理'.format(len(file_list)))
processor = ImageProcessor(font, bold_font)
for file in tqdm(file_list):
# 打开图片
container = ImageContainer(os.path.join(input_dir, file))
# 添加logo
if logo_enable:
container.set_logo(get_logo(container.make))
else:
container.set_logo(None)
# 添加水印
if 'normal_with_right_logo' == layout.type:
watermark = processor.normal_watermark(container, layout, is_logo_left=False)
elif 'square' == layout.type:
watermark = processor.square_watermark(container)
else:
watermark = processor.normal_watermark(container, layout)
# 添加白框
if white_margin_enable:
watermark = padding_image(watermark, int(white_margin_width * container.width / 100), 'tlr')
# 保存图片
watermark.save(os.path.join(output_dir, file), quality=quality)
container.close()
watermark.close()
print('处理完成,文件已输出至 output 文件夹中,请点击任意键退出或直接关闭'.format(len(file_list)))
input()
state = -1
def modify_layout():
"""
状态2:修改布局
:return:
"""
global state
while True:
print('输入【数字】选择布局类型:')
print(' 【1】: normal')
print(' 【2】: normal_with_right_logo')
print(' 【3】: square')
print('输入【0】返回主菜单')
print('输入【x】退出程序')
user_input = input()
if user_input == 'x' or user_input == 'X':
exit(0)
if user_input.isdigit():
if user_input == '0':
return
elif user_input == '1':
config['layout']['type'] = 'normal'
return
elif user_input == '2':
config['layout']['type'] = 'normal_with_right_logo'
return
elif user_input == '3':
config['layout']['type'] = 'square'
return
else:
print('输入错误,请重新输入')
else:
print('输入错误,请重新输入')
def modify_logo():
"""
状态3:修改 logo
:return:
"""
global state
while True:
print('输入【数字】选择是否显示 logo:')
print(' 【1】: 显示 logo')
print(' 【2】: 不显示 logo')
print('输入【0】返回主菜单')
print('输入【x】退出程序')
user_input = input()
if user_input == 'x' or user_input == 'X':
exit(0)
if user_input.isdigit():
if user_input == '0':
return
elif user_input == '1':
config['logo']['enable'] = True
return
elif user_input == '2':
config['logo']['enable'] = False
return
else:
print('输入错误,请重新输入')
else:
print('输入错误,请重新输入')
def modify_element(key):
"""
状态4-7:修改 logo
:return:
"""
global state
while True:
print('输入【数字】选择【{}】显示内容:'.format(id_to_loc[key]))
print(' 【1】:相机型号')
print(' 【2】:相机厂商')
print(' 【3】:镜头型号')
print(' 【4】:拍摄参数')
print(' 【5】:拍摄日期')
print(' 【6】:自定义字段')
print(' 【7】:无')
print('输入【0】返回主菜单')
print('输入【x】退出程序')
user_input = input()
if user_input == 'x' or user_input == 'X':
exit(0)
if user_input.isdigit():
number = int(user_input)
if number == 0:
return
elif number == 1:
config['layout']['elements'][key]['name'] = 'Model'
return
elif number == 2:
config['layout']['elements'][key]['name'] = 'Make'
return
elif number == 3:
config['layout']['elements'][key]['name'] = 'LensModel'
return
elif number == 4:
config['layout']['elements'][key]['name'] = 'Param'
return
elif number == 5:
config['layout']['elements'][key]['name'] = 'Date'
return
elif number == 6:
config['layout']['elements'][key]['name'] = 'Custom'
user_input = input('输入自定义字段的值:\n')
config['layout']['elements'][key]['value'] = user_input
return
elif number == 7:
config['layout']['elements'][key]['name'] = 'None'
return
else:
print('输入错误,请重新输入')
else:
print('输入错误,请重新输入')
state = 0
if __name__ == '__main__':
while True:
if state == 0:
print_current_setting()
elif state == 100:
# 处理数据的代码
print(s_line)
load_config()
processing()
elif state == 1:
modify_layout()
# 修改布局类型的代码
state = 0
elif state == 2:
modify_logo()
# 修改logo的代码
state = 0
elif state == 3:
modify_element('left_top')
# 修改左上文字的代码
state = 0
elif state == 4:
modify_element('left_bottom')
# 修改左下文字的代码
state = 0
elif state == 5:
modify_element('right_top')
# 修改右上文字的代码
state = 0
elif state == 6:
modify_element('right_bottom')
# 修改右下文字的代码
state = 0
elif state == -1:
exit(0)
else:
print("输入错误,请重新输入")
state = 0
save_config()