diff --git a/demo/processor.py b/demo/processor.py index 2df08941..600c3b43 100644 --- a/demo/processor.py +++ b/demo/processor.py @@ -55,8 +55,12 @@ def process( ): # 初始化参数 top_distance_min = top_distance_max - 0.02 + # 得到render_option在LOCALES["render_mode"][language]["choices"]中的索引 + render_option_index = LOCALES["render_mode"][language]["choices"].index( + render_option + ) idphoto_json = self._initialize_idphoto_json( - mode_option, color_option, render_option, image_kb_options + mode_option, color_option, render_option_index, image_kb_options ) # 处理尺寸模式 @@ -296,7 +300,7 @@ def _process_generated_photo( def _render_background(self, result_image_standard, result_image_hd, idphoto_json): """渲染背景""" render_modes = {0: "pure_color", 1: "updown_gradient", 2: "center_gradient"} - render_mode = render_modes.get(idphoto_json["render_mode"], "pure_color") + render_mode = render_modes[idphoto_json["render_mode"]] result_image_standard = np.uint8( add_background( @@ -391,7 +395,11 @@ def _resize_image_if_needed(self, result_image_standard, idphoto_json): result_image_standard, output_image_path, idphoto_json["custom_image_kb"], - dpi=idphoto_json["custom_image_dpi"], + dpi=( + idphoto_json["custom_image_dpi"] + if idphoto_json["custom_image_dpi"] + else 300 + ), ) return output_image_path # 如果只设置了dpi diff --git a/demo/ui.py b/demo/ui.py index fbe5b862..77ff8c58 100644 --- a/demo/ui.py +++ b/demo/ui.py @@ -472,19 +472,24 @@ def change_language(language): ), } + def change_visibility(option, lang, locales_key, custom_component): + return { + custom_component: gr.update( + visible=option == LOCALES[locales_key][lang]["choices"][-1] + ) + } + def change_color(colors, lang): - if colors == LOCALES["bg_color"][lang]["choices"][-1]: - return {custom_color: gr.update(visible=True)} - else: - return {custom_color: gr.update(visible=False)} + return change_visibility(colors, lang, "bg_color", custom_color) def change_size_mode(size_option_item, lang): - if size_option_item == LOCALES["size_mode"][lang]["choices"][2]: + choices = LOCALES["size_mode"][lang]["choices"] + if size_option_item == choices[2]: return { custom_size: gr.update(visible=True), size_list_row: gr.update(visible=False), } - elif size_option_item == LOCALES["size_mode"][lang]["choices"][1]: + elif size_option_item == choices[1]: return { custom_size: gr.update(visible=False), size_list_row: gr.update(visible=False), @@ -496,16 +501,14 @@ def change_size_mode(size_option_item, lang): } def change_image_kb(image_kb_option, lang): - if image_kb_option == LOCALES["image_kb"][lang]["choices"][1]: - return {custom_image_kb_size: gr.update(visible=True)} - else: - return {custom_image_kb_size: gr.update(visible=False)} + return change_visibility( + image_kb_option, lang, "image_kb", custom_image_kb_size + ) def change_image_dpi(image_dpi_option, lang): - if image_dpi_option == LOCALES["image_dpi"][lang]["choices"][1]: - return {custom_image_dpi_size: gr.update(visible=True)} - else: - return {custom_image_dpi_size: gr.update(visible=False)} + return change_visibility( + image_dpi_option, lang, "image_dpi", custom_image_dpi_size + ) # ---------------- 绑定事件 ---------------- # 语言切换