Spaces:
Running
Running
TheEeeeLin
commited on
Commit
·
0a3d586
1
Parent(s):
09383f1
update
Browse files- demo/locales.py +4 -4
- demo/processor.py +81 -40
- demo/ui.py +17 -8
demo/locales.py
CHANGED
@@ -182,22 +182,22 @@ LOCALES = {
|
|
182 |
"bg_color": {
|
183 |
"en": {
|
184 |
"label": "Background color",
|
185 |
-
"choices": list(color_list_dict_EN.keys()) + ["Custom"],
|
186 |
"develop": color_list_dict_EN,
|
187 |
},
|
188 |
"zh": {
|
189 |
"label": "背景颜色",
|
190 |
-
"choices": list(color_list_dict_CN.keys()) + ["
|
191 |
"develop": color_list_dict_CN,
|
192 |
},
|
193 |
"ja": {
|
194 |
"label": "背景色",
|
195 |
-
"choices": list(color_list_dict_EN.keys()) + ["カスタム"],
|
196 |
"develop": color_list_dict_EN,
|
197 |
},
|
198 |
"ko": {
|
199 |
"label": "배경색",
|
200 |
-
"choices": list(color_list_dict_EN.keys()) + ["사용자 지정"],
|
201 |
"develop": color_list_dict_EN,
|
202 |
},
|
203 |
},
|
|
|
182 |
"bg_color": {
|
183 |
"en": {
|
184 |
"label": "Background color",
|
185 |
+
"choices": list(color_list_dict_EN.keys()) + ["Custom(RGB)", "Custom(HEX)"],
|
186 |
"develop": color_list_dict_EN,
|
187 |
},
|
188 |
"zh": {
|
189 |
"label": "背景颜色",
|
190 |
+
"choices": list(color_list_dict_CN.keys()) + ["自定义(RGB)", "自定义(HEX)"],
|
191 |
"develop": color_list_dict_CN,
|
192 |
},
|
193 |
"ja": {
|
194 |
"label": "背景色",
|
195 |
+
"choices": list(color_list_dict_EN.keys()) + ["カスタム(RGB)", "カスタム(HEX)"],
|
196 |
"develop": color_list_dict_EN,
|
197 |
},
|
198 |
"ko": {
|
199 |
"label": "배경색",
|
200 |
+
"choices": list(color_list_dict_EN.keys()) + ["사용자 지정(RGB)", "사용자 지정(HEX)"],
|
201 |
"develop": color_list_dict_EN,
|
202 |
},
|
203 |
},
|
demo/processor.py
CHANGED
@@ -31,6 +31,7 @@ class IDPhotoProcessor:
|
|
31 |
custom_color_R,
|
32 |
custom_color_G,
|
33 |
custom_color_B,
|
|
|
34 |
custom_size_height,
|
35 |
custom_size_width,
|
36 |
custom_size_height_mm,
|
@@ -88,6 +89,7 @@ class IDPhotoProcessor:
|
|
88 |
custom_color_R,
|
89 |
custom_color_G,
|
90 |
custom_color_B,
|
|
|
91 |
)
|
92 |
|
93 |
# 如果设置了自定义KB大小
|
@@ -208,12 +210,28 @@ class IDPhotoProcessor:
|
|
208 |
custom_color_R,
|
209 |
custom_color_G,
|
210 |
custom_color_B,
|
|
|
211 |
):
|
212 |
"""处理颜色模式"""
|
213 |
-
|
|
|
214 |
idphoto_json["color_bgr"] = tuple(
|
215 |
map(range_check, [custom_color_R, custom_color_G, custom_color_B])
|
216 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
217 |
else:
|
218 |
hex_color = LOCALES["bg_color"][language]["develop"][color_option]
|
219 |
idphoto_json["color_bgr"] = tuple(
|
@@ -288,7 +306,7 @@ class IDPhotoProcessor:
|
|
288 |
)
|
289 |
|
290 |
# 生成排版照片
|
291 |
-
result_image_layout,
|
292 |
idphoto_json,
|
293 |
result_image_standard,
|
294 |
language,
|
@@ -315,21 +333,41 @@ class IDPhotoProcessor:
|
|
315 |
)
|
316 |
|
317 |
# 调整图片大小
|
318 |
-
|
319 |
result_image_standard,
|
320 |
result_image_hd,
|
321 |
result_image_layout,
|
322 |
idphoto_json,
|
323 |
)
|
324 |
|
325 |
-
|
326 |
-
|
327 |
-
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
333 |
|
334 |
def _render_background(self, result_image_standard, result_image_hd, idphoto_json):
|
335 |
"""渲染背景"""
|
@@ -363,8 +401,9 @@ class IDPhotoProcessor:
|
|
363 |
watermark_text_color,
|
364 |
):
|
365 |
"""生成排版照片"""
|
|
|
366 |
if idphoto_json["size_mode"] in LOCALES["size_mode"][language]["choices"][1]:
|
367 |
-
return None,
|
368 |
|
369 |
typography_arr, typography_rotate = generate_layout_photo(
|
370 |
input_height=idphoto_json["size"][0],
|
@@ -391,10 +430,7 @@ class IDPhotoProcessor:
|
|
391 |
width=idphoto_json["size"][1],
|
392 |
)
|
393 |
|
394 |
-
return result_image_layout,
|
395 |
-
value=result_image_layout,
|
396 |
-
visible=True,
|
397 |
-
)
|
398 |
|
399 |
def _add_watermark(
|
400 |
self,
|
@@ -436,9 +472,9 @@ class IDPhotoProcessor:
|
|
436 |
)
|
437 |
timestamp = int(time.time())
|
438 |
output_paths = {
|
439 |
-
"standard": f"{base_path}/{timestamp}_standard",
|
440 |
-
"hd": f"{base_path}/{timestamp}_hd",
|
441 |
-
"layout": f"{base_path}/{timestamp}_layout",
|
442 |
}
|
443 |
|
444 |
# 获取自定义的KB和DPI值
|
@@ -449,50 +485,59 @@ class IDPhotoProcessor:
|
|
449 |
if custom_kb and custom_dpi:
|
450 |
# 为所有输出路径添加DPI信息
|
451 |
for key in output_paths:
|
452 |
-
output_paths[key] += f"_{custom_dpi}dpi.jpg"
|
453 |
# 为标准图像添加KB信息
|
454 |
-
output_paths["standard"] = output_paths["standard"].replace(
|
455 |
".jpg", f"_{custom_kb}kb.jpg"
|
456 |
)
|
457 |
|
458 |
# 调整标准图像大小并保存
|
459 |
resize_image_to_kb(
|
460 |
result_image_standard,
|
461 |
-
output_paths["standard"],
|
462 |
custom_kb,
|
463 |
dpi=custom_dpi,
|
464 |
)
|
|
|
465 |
# 保存高清图像和排版图像
|
466 |
-
save_image_dpi_to_bytes(result_image_hd, output_paths["hd"], dpi=custom_dpi)
|
467 |
-
|
468 |
-
|
469 |
-
|
|
|
|
|
|
|
470 |
|
471 |
-
return
|
472 |
|
473 |
# 只有自定义DPI的情况
|
474 |
elif custom_dpi:
|
475 |
for key in output_paths:
|
476 |
-
output_paths[key] += f"_{custom_dpi}dpi.jpg"
|
477 |
# 保存所有图像,使用自定义DPI
|
478 |
-
|
479 |
-
|
480 |
-
|
|
|
|
|
|
|
|
|
481 |
|
482 |
-
return
|
483 |
|
484 |
# 只有自定义KB的情况
|
485 |
elif custom_kb:
|
486 |
-
output_paths["standard"] += f"_{custom_kb}kb.jpg"
|
487 |
# 只调整标准图像大小并保存
|
488 |
resize_image_to_kb(
|
489 |
result_image_standard,
|
490 |
-
output_paths["standard"],
|
491 |
custom_kb,
|
492 |
dpi=300,
|
493 |
)
|
|
|
494 |
|
495 |
-
return
|
496 |
|
497 |
# 如果没有自定义设置,返回None
|
498 |
return None
|
@@ -504,7 +549,6 @@ class IDPhotoProcessor:
|
|
504 |
result_image_standard_png,
|
505 |
result_image_hd_png,
|
506 |
result_layout_image,
|
507 |
-
output_image_path,
|
508 |
):
|
509 |
"""创建响应"""
|
510 |
response = [
|
@@ -515,10 +559,7 @@ class IDPhotoProcessor:
|
|
515 |
result_layout_image,
|
516 |
gr.update(visible=False),
|
517 |
]
|
518 |
-
|
519 |
-
response.append(gr.update(visible=True, value=output_image_path))
|
520 |
-
else:
|
521 |
-
response.append(gr.update(visible=False))
|
522 |
return response
|
523 |
|
524 |
def _create_error_response(self, language):
|
|
|
31 |
custom_color_R,
|
32 |
custom_color_G,
|
33 |
custom_color_B,
|
34 |
+
custom_color_hex_value,
|
35 |
custom_size_height,
|
36 |
custom_size_width,
|
37 |
custom_size_height_mm,
|
|
|
89 |
custom_color_R,
|
90 |
custom_color_G,
|
91 |
custom_color_B,
|
92 |
+
custom_color_hex_value,
|
93 |
)
|
94 |
|
95 |
# 如果设置了自定义KB大小
|
|
|
210 |
custom_color_R,
|
211 |
custom_color_G,
|
212 |
custom_color_B,
|
213 |
+
custom_color_hex_value,
|
214 |
):
|
215 |
"""处理颜色模式"""
|
216 |
+
# 如果选择了自定义颜色
|
217 |
+
if idphoto_json["color_mode"] == LOCALES["bg_color"][language]["choices"][-2]:
|
218 |
idphoto_json["color_bgr"] = tuple(
|
219 |
map(range_check, [custom_color_R, custom_color_G, custom_color_B])
|
220 |
)
|
221 |
+
elif idphoto_json["color_mode"] == LOCALES["bg_color"][language]["choices"][-1]:
|
222 |
+
hex_color = custom_color_hex_value
|
223 |
+
# 将十六进制颜色转换为RGB颜色,如果长度为6,则直接转换,如果长度为7,则去掉#号再转换
|
224 |
+
if len(hex_color) == 6:
|
225 |
+
idphoto_json["color_bgr"] = tuple(
|
226 |
+
int(hex_color[i : i + 2], 16) for i in (0, 2, 4)
|
227 |
+
)
|
228 |
+
elif len(hex_color) == 7:
|
229 |
+
hex_color = hex_color[1:]
|
230 |
+
idphoto_json["color_bgr"] = tuple(
|
231 |
+
int(hex_color[i : i + 2], 16) for i in (0, 2, 4)
|
232 |
+
)
|
233 |
+
else:
|
234 |
+
raise ValueError("Invalid hex color. You can only use 6 or 7 characters. For example: #FFFFFF or FFFFFF")
|
235 |
else:
|
236 |
hex_color = LOCALES["bg_color"][language]["develop"][color_option]
|
237 |
idphoto_json["color_bgr"] = tuple(
|
|
|
306 |
)
|
307 |
|
308 |
# 生成排版照片
|
309 |
+
result_image_layout, result_image_layout_visible = self._generate_image_layout(
|
310 |
idphoto_json,
|
311 |
result_image_standard,
|
312 |
language,
|
|
|
333 |
)
|
334 |
|
335 |
# 调整图片大小
|
336 |
+
output_image_path_dict = self._resize_image_if_needed(
|
337 |
result_image_standard,
|
338 |
result_image_hd,
|
339 |
result_image_layout,
|
340 |
idphoto_json,
|
341 |
)
|
342 |
|
343 |
+
# 如果output_image_path_dict为None,
|
344 |
+
if output_image_path_dict is None:
|
345 |
+
return self._create_response(
|
346 |
+
result_image_standard,
|
347 |
+
result_image_hd,
|
348 |
+
result_image_standard_png,
|
349 |
+
result_image_hd_png,
|
350 |
+
gr.update(value=result_image_layout, visible=result_image_layout_visible),
|
351 |
+
)
|
352 |
+
else:
|
353 |
+
# 如果output_image_path_dict不为None,
|
354 |
+
if output_image_path_dict["layout"]["processed"]:
|
355 |
+
result_image_layout = output_image_path_dict["layout"]["path"]
|
356 |
+
return self._create_response(
|
357 |
+
(
|
358 |
+
output_image_path_dict["standard"]["path"]
|
359 |
+
if output_image_path_dict["standard"]["processed"]
|
360 |
+
else result_image_standard
|
361 |
+
),
|
362 |
+
(
|
363 |
+
output_image_path_dict["hd"]["path"]
|
364 |
+
if output_image_path_dict["hd"]["processed"]
|
365 |
+
else result_image_hd
|
366 |
+
),
|
367 |
+
result_image_standard_png,
|
368 |
+
result_image_hd_png,
|
369 |
+
gr.update(value=result_image_layout, visible=result_image_layout_visible),
|
370 |
+
)
|
371 |
|
372 |
def _render_background(self, result_image_standard, result_image_hd, idphoto_json):
|
373 |
"""渲染背景"""
|
|
|
401 |
watermark_text_color,
|
402 |
):
|
403 |
"""生成排版照片"""
|
404 |
+
# 如果选择了自定义尺寸,则不生成排版照片
|
405 |
if idphoto_json["size_mode"] in LOCALES["size_mode"][language]["choices"][1]:
|
406 |
+
return None, False
|
407 |
|
408 |
typography_arr, typography_rotate = generate_layout_photo(
|
409 |
input_height=idphoto_json["size"][0],
|
|
|
430 |
width=idphoto_json["size"][1],
|
431 |
)
|
432 |
|
433 |
+
return result_image_layout, True
|
|
|
|
|
|
|
434 |
|
435 |
def _add_watermark(
|
436 |
self,
|
|
|
472 |
)
|
473 |
timestamp = int(time.time())
|
474 |
output_paths = {
|
475 |
+
"standard": {"path": f"{base_path}/{timestamp}_standard", "processed": False},
|
476 |
+
"hd": {"path": f"{base_path}/{timestamp}_hd", "processed": False},
|
477 |
+
"layout": {"path": f"{base_path}/{timestamp}_layout", "processed": False},
|
478 |
}
|
479 |
|
480 |
# 获取自定义的KB和DPI值
|
|
|
485 |
if custom_kb and custom_dpi:
|
486 |
# 为所有输出路径添加DPI信息
|
487 |
for key in output_paths:
|
488 |
+
output_paths[key]["path"] += f"_{custom_dpi}dpi.jpg"
|
489 |
# 为标准图像添加KB信息
|
490 |
+
output_paths["standard"]["path"] = output_paths["standard"]["path"].replace(
|
491 |
".jpg", f"_{custom_kb}kb.jpg"
|
492 |
)
|
493 |
|
494 |
# 调整标准图像大小并保存
|
495 |
resize_image_to_kb(
|
496 |
result_image_standard,
|
497 |
+
output_paths["standard"]["path"],
|
498 |
custom_kb,
|
499 |
dpi=custom_dpi,
|
500 |
)
|
501 |
+
output_paths["standard"]["processed"] = True
|
502 |
# 保存高清图像和排版图像
|
503 |
+
save_image_dpi_to_bytes(result_image_hd, output_paths["hd"]["path"], dpi=custom_dpi)
|
504 |
+
output_paths["hd"]["processed"] = True
|
505 |
+
if result_image_layout:
|
506 |
+
save_image_dpi_to_bytes(
|
507 |
+
result_image_layout, output_paths["layout"]["path"], dpi=custom_dpi
|
508 |
+
)
|
509 |
+
output_paths["layout"]["processed"] = True
|
510 |
|
511 |
+
return output_paths
|
512 |
|
513 |
# 只有自定义DPI的情况
|
514 |
elif custom_dpi:
|
515 |
for key in output_paths:
|
516 |
+
output_paths[key]["path"] += f"_{custom_dpi}dpi.jpg"
|
517 |
# 保存所有图像,使用自定义DPI
|
518 |
+
if key == "layout" and result_image_layout is None:
|
519 |
+
pass
|
520 |
+
else:
|
521 |
+
save_image_dpi_to_bytes(
|
522 |
+
locals()[f"result_image_{key}"], output_paths[key]["path"], dpi=custom_dpi
|
523 |
+
)
|
524 |
+
output_paths[key]["processed"] = True
|
525 |
|
526 |
+
return output_paths
|
527 |
|
528 |
# 只有自定义KB的情况
|
529 |
elif custom_kb:
|
530 |
+
output_paths["standard"]["path"] += f"_{custom_kb}kb.jpg"
|
531 |
# 只调整标准图像大小并保存
|
532 |
resize_image_to_kb(
|
533 |
result_image_standard,
|
534 |
+
output_paths["standard"]["path"],
|
535 |
custom_kb,
|
536 |
dpi=300,
|
537 |
)
|
538 |
+
output_paths["standard"]["processed"] = True
|
539 |
|
540 |
+
return output_paths
|
541 |
|
542 |
# 如果没有自定义设置,返回None
|
543 |
return None
|
|
|
549 |
result_image_standard_png,
|
550 |
result_image_hd_png,
|
551 |
result_layout_image,
|
|
|
552 |
):
|
553 |
"""创建响应"""
|
554 |
response = [
|
|
|
559 |
result_layout_image,
|
560 |
gr.update(visible=False),
|
561 |
]
|
562 |
+
|
|
|
|
|
|
|
563 |
return response
|
564 |
|
565 |
def _create_error_response(self, language):
|
demo/ui.py
CHANGED
@@ -120,11 +120,16 @@ def create_ui(
|
|
120 |
label=LOCALES["bg_color"][DEFAULT_LANG]["label"],
|
121 |
value=LOCALES["bg_color"][DEFAULT_LANG]["choices"][0],
|
122 |
)
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
|
|
|
|
|
|
|
|
|
|
128 |
|
129 |
render_options = gr.Radio(
|
130 |
choices=LOCALES["render_mode"][DEFAULT_LANG]["choices"],
|
@@ -535,7 +540,11 @@ def create_ui(
|
|
535 |
}
|
536 |
|
537 |
def change_color(colors, lang):
|
538 |
-
return
|
|
|
|
|
|
|
|
|
539 |
|
540 |
def change_size_mode(size_option_item, lang):
|
541 |
choices = LOCALES["size_mode"][lang]["choices"]
|
@@ -650,7 +659,7 @@ def create_ui(
|
|
650 |
color_options.input(
|
651 |
change_color,
|
652 |
inputs=[color_options, language_options],
|
653 |
-
outputs=[
|
654 |
)
|
655 |
|
656 |
# 图片kb
|
@@ -679,6 +688,7 @@ def create_ui(
|
|
679 |
custom_color_R,
|
680 |
custom_color_G,
|
681 |
custom_color_B,
|
|
|
682 |
custom_size_height_px,
|
683 |
custom_size_width_px,
|
684 |
custom_size_height_mm,
|
@@ -712,7 +722,6 @@ def create_ui(
|
|
712 |
img_output_standard_hd_png,
|
713 |
img_output_layout,
|
714 |
notification,
|
715 |
-
file_download,
|
716 |
],
|
717 |
)
|
718 |
|
|
|
120 |
label=LOCALES["bg_color"][DEFAULT_LANG]["label"],
|
121 |
value=LOCALES["bg_color"][DEFAULT_LANG]["choices"][0],
|
122 |
)
|
123 |
+
|
124 |
+
# 自定义颜色RGB
|
125 |
+
with gr.Row(visible=False) as custom_color_rgb:
|
126 |
+
custom_color_R = gr.Number(value=0, label="R", minimum=0, maximum=255, interactive=True)
|
127 |
+
custom_color_G = gr.Number(value=0, label="G", minimum=0, maximum=255, interactive=True)
|
128 |
+
custom_color_B = gr.Number(value=0, label="B", minimum=0, maximum=255, interactive=True)
|
129 |
+
|
130 |
+
# 自定义颜色HEX
|
131 |
+
with gr.Row(visible=False) as custom_color_hex:
|
132 |
+
custom_color_hex_value = gr.Text(value="000000", label="Hex", interactive=True)
|
133 |
|
134 |
render_options = gr.Radio(
|
135 |
choices=LOCALES["render_mode"][DEFAULT_LANG]["choices"],
|
|
|
540 |
}
|
541 |
|
542 |
def change_color(colors, lang):
|
543 |
+
return {
|
544 |
+
custom_color_rgb: gr.update(visible = colors == LOCALES["bg_color"][lang]["choices"][-2]),
|
545 |
+
custom_color_hex: gr.update(visible = colors == LOCALES["bg_color"][lang]["choices"][-1]),
|
546 |
+
}
|
547 |
+
|
548 |
|
549 |
def change_size_mode(size_option_item, lang):
|
550 |
choices = LOCALES["size_mode"][lang]["choices"]
|
|
|
659 |
color_options.input(
|
660 |
change_color,
|
661 |
inputs=[color_options, language_options],
|
662 |
+
outputs=[custom_color_rgb, custom_color_hex],
|
663 |
)
|
664 |
|
665 |
# 图片kb
|
|
|
688 |
custom_color_R,
|
689 |
custom_color_G,
|
690 |
custom_color_B,
|
691 |
+
custom_color_hex_value,
|
692 |
custom_size_height_px,
|
693 |
custom_size_width_px,
|
694 |
custom_size_height_mm,
|
|
|
722 |
img_output_standard_hd_png,
|
723 |
img_output_layout,
|
724 |
notification,
|
|
|
725 |
],
|
726 |
)
|
727 |
|